Activity中bitmap的回收

1.Activity中Bitmap的回收

public void recycleAllImages () {

         
         Logger.d("---------recycleView=====::" + getClass().getSimpleName());
         
         View decorView = getWindow().getDecorView();
         recycleViewImages(decorView);
        }

       

2.View中Bitmap的回收

        public void recycleViewImages(View view) {
         
         if (view == null) {
          return ;
         }
         
         Logger.d("---------recycleView=====:" + view.getClass().getSimpleName());
         
       
         Drawable bgDrawable = view.getBackground();
      if (bgDrawable != null && bgDrawable instanceof BitmapDrawable) {
       BitmapDrawable bd = (BitmapDrawable) bgDrawable;
       if (bd != null) {
        if (bd.getBitmap() != null) {
         bd.getBitmap().recycle();
        }
       }
      }
         
  
         if (view instanceof ViewGroup) {
          ViewGroup group = (ViewGroup) view;
          for (int i=0; i            recycleViewImages(group.getChildAt(i));
          }
          
        
         } else {
          if (view instanceof ImageView || view instanceof ImageButton) {
           ImageView iv = (ImageView) view;
           Drawable drawable = iv.getDrawable();
           if (drawable != null && drawable instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            if (bd != null) {
             if (bd.getBitmap() != null) {
              bd.getBitmap().recycle();
             }
            }
           }
          }
         }
        }

你可能感兴趣的:(android)