Question

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?

Was it helpful?

Solution

I had the same issue, the problem was the runnable. Making an independent thread to each animation made animationDrawables persist after destroying the activity and clearing views and callbacks. So starting animations without independent threads lets GC collect unused resources.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top