我有一个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);
}为什么它不重新初始化整个事情呢?
发布于 2015-09-09 08:40:22
问题在于,当在位图上调用Bitmap.recycle()时,不能重用它。
此外,在调用setBackgroundResources(R.drawable.sensor_animation)时,您所引用的对象与其位图以前被回收的对象相同。
作为一种解决方案,您必须每次创建一个新的可绘制对象,或者不回收该实例。
如果您担心内存的使用,请使用较小的位图。同样,对于不同的屏幕密度,使用正确的尺寸也会有所帮助。
https://stackoverflow.com/questions/32463736
复制相似问题