Android 从AnimationDrawable中释放内存

Android 从AnimationDrawable中释放内存,android,animationdrawable,Android,Animationdrawable,我在我的应用程序的许多活动中使用animationdrawables。以下是将其取消标记的代码: public void prepareVideo (int resourceBall, String animation, int width, int height ){ imgBall = (ImageView)findViewById(resourceBall); imgBall.setVisibility(ImageView.VISIBLE); String re

我在我的应用程序的许多活动中使用animationdrawables。以下是将其取消标记的代码:

 public void prepareVideo (int resourceBall, String animation, int width, int height ){

    imgBall = (ImageView)findViewById(resourceBall);
    imgBall.setVisibility(ImageView.VISIBLE);
    String resourceNameAnimation = animation;
    int id_res = getResources().getIdentifier(resourceNameAnimation, "drawable", getPackageName());
    imgBall.setBackgroundResource(id_res);
        LayoutParams latoutFrame = imgBall.getLayoutParams();
    latoutFrame.width = width;
    latoutFrame.height = height;
    imgBall.setLayoutParams(latoutFrame); 
    frame = (AnimationDrawable) imgBall.getBackground();


}
然后我叫: imgBall.post(newstartanimation())

这是一个可运行的调用:

class StartAnimation implements Runnable {

    public void run() {

frame.start();

    }

}
问题是我使用了许多相同的动画,它们使用相同的xml(逐帧)。我必须在许多具有相同动画的活动中调用此方法。最后我得到了一个OutOfMemory错误。我正在尝试在屏幕之间释放内存:

for (int i = 0; i < frame.getNumberOfFrames(); ++i){
     Drawable frame_rescue = frame.getFrame(i);
     if (frame_rescue instanceof BitmapDrawable) {
         ((BitmapDrawable)frame_rescue).getBitmap().recycle();
     }
     frame_rescue.setCallback(null);
for(int i=0;i
问题是,当我再次尝试使用资源时,会出现其他错误: “正在尝试使用回收的位图”


有人知道其他释放内存的方法吗?

我也有同样的问题,问题是可运行性。 为每个动画创建一个独立的线程,使animationDrawables在销毁活动并清除视图和回调后保持不变。 因此,在没有独立线程的情况下启动动画可以让GC收集未使用的资源