回收ImageView占用的圖像內存


 

使用方法: RecycleBitmap.recycleImageView(mSelectorView);   參數為imageview

/**
* 回收ImageView占用的圖像內存;
* 使用了本方法之后,記得用一下
* System.gc(); 提醒系統及時回收

* @param view
* 在setImageResource()方法前面還沒有bitmap,圖片可以出來。
* 在其后面執行本方法的話,圖片就不會顯示出來。
* 在setImageBitmap()方法的前面還沒有bitmap,圖片可以出來。
* 在其后面執行本方法的話,圖片就不會顯示出來。
* 最好在onDestroy方法中執行本方法
*/

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();
                    log("have recycled ImageView Bitmap");
                    bmp=null;
                }
            }
        }
    }

 

 

 

使用方法:  RecycleBitmap.recycleBitmap(mNo_shop1Bitmap);  參數為bitmap

/**
* 可以釋放到Bitmap在C層申請的內存,recycle方法會把一些相關的引用計數置0.
* 回收Bitmap占用的圖像內存;
* 使用了本方法之后,記得用一下
* System.gc(); 提醒系統及時回收
* @param bitmap
* 在setImageBitmap()方法的前面和后面都會報異常,最好在onDestroy方法中執行
* RuntimeException:trying to use a recycled bitmap android.graphics.Bitmap@41c124e8
*/

 public static void recycleBitmap(Bitmap bitmap){
            if(bitmap!=null&&!bitmap.isRecycled()){
               bitmap.recycle() ;  //回收圖片所占的內存
               log("have recycled Bitmap");
               bitmap=null;
         }
    }

 

 

 

 

/**
* 回收List<Drawable>中的drawable
*/

private void recycleDrawable(List<Drawable> listDrawable) {
        if(listDrawable==null){
            return;
        }
        BitmapDrawable bd;
        Bitmap bitmap;
        for(Drawable drawable:listDrawable){
            if(drawable!=null){
                bd = (BitmapDrawable) drawable;
                bitmap =bd.getBitmap(); 
                if(bitmap!=null&&!bitmap.isRecycled()){
                    bitmap.recycle() ;  //回收圖片所占的內存
                    log("have recycled Bitmap");
                    bitmap=null;
                }
            }
        }
        listDrawable.clear();//它清空了,外面的集合也會清空
        listDrawable=null;//它等於null,外面的集合不等於null
 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM