recycle ImageView Animation Drawable - Android User Interface

Android examples for User Interface:ImageView

Description

recycle ImageView Animation Drawable

Demo Code


import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class Main{
    private static final String TAG = "ForceGCUtils";
    /*  w ww  .  j a va2s.  c o  m*/
    public static void recycleViewAnimationDrawable(ImageView view) {
        if (view == null) {
            return;
        }
        Drawable drawable = view.getBackground();
        if (drawable instanceof AnimationDrawable) {
            AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
            animationDrawable.stop();
            for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
                Drawable frame = animationDrawable.getFrame(i);
                if (frame instanceof BitmapDrawable) {
                    LogUtils.d(TAG,
                            "imageview recycle AnimationDrawable bitmap!");
                    if (SDKVersionUtils.hasJellyBean()) {
                        view.setBackground(null);
                    } else {
                        view.setBackgroundDrawable(null);
                    }
                    ((BitmapDrawable) frame).getBitmap().recycle();
                }
                frame.setCallback(null);
            }
            animationDrawable.setCallback(null);
        }
    }
}

Related Tutorials