2012-05-29 141 views
3

在我的應用程序中,我想交換圖像當用戶點擊它時在運行時。刪除繪圖緩存

有兩個imageviews當用戶點擊第一個圖像,然後在我取第一的ImageView圖像的位圖,並分配給第二imageview的此同一時間,我用點擊第二圖像下面的代碼:

public Bitmap createBitmap(ImageView imageview) { 
    imageview.setDrawingCacheEnabled(true); 
    imageview.buildDrawingCache(false); 

    if(imageview.getDrawingCache() != null) { 
     Bitmap bitmap = Bitmap.createBitmap(imageview.getDrawingCache()); 
     imageview.setDrawingCacheEnabled(false); 
     return bitmap; 
    } else { 
     return null; 
    } 
} 

代碼工作正常,但緩存不清除每次和位圖用前一次緩存創建,所以我怎麼可以清除位圖緩存?

+0

plz使用這個你會得到答https://github.com/thest1/LazyList – himanshu

+0

請選擇你的答案 – breceivemail

回答

2

這是一個例子,例如我在哪裏使用Free the native object associated with this bitmap

Bitmap bitmap; 

public Bitmap createBitmap(ImageView imageview) { 
    if (bitmap != null) { 
     bitmap.recycle(); 
     bitmap = null; 
    } 
    bitmap = Bitmap.createBitmap(imageview.getDrawingCache()); 
    // Your Code of bitmap Follows here 
} 

在使用Bitmap之前,只需釋放該對象。

+0

thnx的答覆...但我想我必須回收位圖之前把價值,糾正我,如果我錯了 – Prachi

+0

@curious_mind:在適當的時候分配位圖時作出的更改。 – Bhavin

+0

蜂巢+位圖不再分配在本機堆中,它們現在在Dalvik堆中。調用recycle()不再需要。 –

1

使用bitmap.recycle();評估您的位圖之前清除它們的緩存,然後重新創建它。