gpt4 book ai didi

Android AnimationDrawable 回收问题

转载 作者:行者123 更新时间:2023-11-30 01:54:36 25 4
gpt4 key购买 nike

我有一个 AnimationDrawable,我在开始动画之前初始化它并在它结束时回收它。问题是当我想再次开始动画时,我重新初始化所有内容但仍然给出异常

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@2bbad018

这是我的代码

 public ImageView radarImageView;
public AnimationDrawable animationDrawable;

public void animationStart() {

// animationStop();

radarImageView = (ImageView) findViewById(R.id.radarIV);
radarImageView.setBackgroundResource(R.drawable.sensor_animation);
animationDrawable = (AnimationDrawable) radarImageView.getBackground();

animationDrawable.start();
}

public void animationStop() {

animationDrawable.stop();

for (int i = 0; i < animationDrawable.getNumberOfFrames(); ++i){
Drawable frame = animationDrawable.getFrame(i);
if (frame instanceof BitmapDrawable) {
((BitmapDrawable)frame).getBitmap().recycle();
}
frame.setCallback(null);
}
animationDrawable.setCallback(null);

// animationDrawable = null;
// radarImageView.setBackgroundResource(0);
}

为什么它不重新初始化整个东西?

最佳答案

问题是因为当您在位图上调用 Bitmap.recycle() 时,您无法重用它。

此外,当您调用 setBackgroundResources(R.drawable.sensor_animation) 时,您指的是之前回收其位图的同一个对象。

作为一种解决方案,您必须每次都创建一个新的可绘制对象,或者不回收该实例。

如果您担心内存使用量,请尝试使用较小的位图。对于不同的屏幕密度使用正确的尺寸也会有所帮助。

关于Android AnimationDrawable 回收问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32463736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com