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<group.getChildCount(); 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();
             }
            }
           }
          }
         }
        }

猜你喜欢

转载自blog.csdn.net/huangrangg12/article/details/19032823