recycle View Group - Android User Interface

Android examples for User Interface:RecyclerView

Description

recycle View Group

Demo Code


//package com.java2s;
import android.graphics.Bitmap;
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 {

    public static void recycleViewGroup(ViewGroup layout) {
        if (layout == null)
            return;
        synchronized (layout) {
            for (int i = 0; i < layout.getChildCount(); i++) {
                View subView = layout.getChildAt(i);
                if (subView instanceof ViewGroup) {
                    recycleViewGroup((ViewGroup) subView);
                } else {
                    if (subView instanceof ImageView) {
                        recycleImageView((ImageView) subView);
                    }//  w  ww  .  j  a v a  2s  . c o  m
                }
            }
        }
    }

    public static void recycleImageView(View view) {
        if (view == null)
            return;
        if (view instanceof ImageView) {
            Drawable drawable = ((ImageView) view).getDrawable();
            if (drawable instanceof BitmapDrawable) {
                Bitmap bmp = ((BitmapDrawable) drawable).getBitmap();
                if (bmp != null && !bmp.isRecycled()) {
                    ((ImageView) view).setImageBitmap(null);
                    bmp.recycle();
                    bmp = null;
                }
            }
        }
    }
}

Related Tutorials