5

I'm using animationdrawables in many activities of my app. This is the code to inicializate them:

 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();


}

Then i call: imgBall.post(new StartAnimation());

that call a runnable:

class StartAnimation implements Runnable {

    public void run() {

frame.start();

    }

}

The problem is i use many animations that are the uses the same xml (frame by frame). I have to call this method in many activities with the same animation. Finally i get a outofmemoryerror. I'm trying to free memory between screens:

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);

The problem is that when i try to use a resourde again y get other error: "Trying to use recycled bitmap"

Anyone know other way of free memory?

4

1 回答 1

0

我有同样的问题,问题是可运行的。为每个动画制作一个独立的线程使得animationDrawables 在销毁活动并清除视图和回调后仍然存在。所以在没有独立线程的情况下启动动画让 GC 收集未使用的资源。

于 2015-07-21T07:07:06.113 回答