android - libgdx 内存分配在动画上失败
问题描述
我正在开发一个 libgdx 项目,从我的屏幕触发了一个扩展演员的动画对象,它适用于少于 50 帧的动画,但是当我尝试使用更高帧数初始化我的动画对象时,应用程序崩溃如果我重新运行应用程序崩溃的动画,则会显示下面所述的错误消息,我曾尝试处理过使用过的图集。这是我正在使用的动画类。
public class AnimatedFullScreen extends Actor {
public boolean startAnimation = true;
public Sound sound;
private OnAnimationComplete mOnAnimationCompleterListener;
private Animation<TextureRegion> animation;
private TextureAtlas atlas;
private float scaleX, scaleY;
private float px = 0, py = 0;
private float animationWidth, animationHeight;
private int type = 0;
private float showTime;
private boolean loop;public AnimatedFullScreen(String atlasName) {
atlas = new TextureAtlas(Gdx.files.internal("animations/sceneatlas/"+atlasName+"/"+atlasName+".atlas"));
this.animation = new Animation<TextureRegion>(0.2f / 10f, atlas.getRegions());
this.scaleX = CommonObjects.screenWidth / atlas.getRegions().get(0).getRegionWidth();
this.scaleY = CommonObjects.screenHeight / atlas.getRegions().get(0).getRegionHeight();
this.loop = false;
this.px = 0;
this.py = 0;
this.startAnimation = true;
this.type = 0;
this.animationWidth =CommonObjects.screenWidth ;
this.animationHeight = CommonObjects.screenHeight;
}
@Override
public void act(float delta) {
super.act(delta);
showTime += delta;
}
@Override
public void draw(Batch batch, float parentAlpha) {
if (startAnimation) {
batch.draw(animation.getKeyFrame(showTime, loop), px, py, 0, 0,
type == 0 ? animation.getKeyFrame(showTime).getRegionWidth() : animationWidth,
type == 0 ? animation.getKeyFrame(showTime).getRegionHeight() : animationHeight,
type == 0 ? scaleX : scaleY,
type == 0 ? scaleY : scaleY, 0);
}
//shows still image when animation not started
else {
batch.draw(animation.getKeyFrame(0, loop), px, py, 0, 0,
type == 0 ? animation.getKeyFrame(showTime).getRegionWidth() : animationWidth,
type == 0 ? animation.getKeyFrame(showTime).getRegionHeight() : animationHeight,
type == 0 ? scaleX : scaleY,
type == 0 ? scaleY : scaleY, 0);
}
//fires listener when animation completed
if (animation.isAnimationFinished(showTime)) {
if (mOnAnimationCompleterListener != null) {
atlas.dispose();
Gdx.app.log("ssa","ddss");
mOnAnimationCompleterListener.setNextScreen();
}
}
}
public void setListener(OnAnimationComplete listener) {
mOnAnimationCompleterListener = listener;
}}
这是错误日志
07-11 11:51:10.209 28389-28485/? W/Adreno-GSL: : sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory 07-11 11:51:10.233 28389-28485/? E/Adreno-GSL: : GSL MEM ERROR: kgsl_sharedmem_alloc ioctl 失败。
解决方案
libGDX 中动画允许的帧数没有技术限制。唯一的限制是内存。所以,如果你有内存分配错误,那么你有太多的对象。您应该考虑优化动画以减少帧数,或优化涉及的精灵以降低内存。还要检查所有其他正在使用的图形,看看是否可以优化图形。你没有提到你是否是,但如果你不是,请确保使用精灵表来帮助内存使用。尽量减少浪费的空间量以降低内存使用量。