1

我有一个 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);
    }

为什么它不再重新初始化整个事情?

4

1 回答 1

1

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

此外,正如您所说setBackgroundResources(R.drawable.sensor_animation),您指的是之前回收其位图的同一对象。

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

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

于 2015-09-09T08:40:22.323 回答