這個問題與Do we have to explicitly recycle the bitmap if we don't need it?。相關。如何在不需要的情況下銷燬ImageView中的drawable?
有一個ImageView有一個drawable,當用戶點擊一個按鈕時,它會爲ImageView分配一個新的drawable。
我們是否必須銷燬屬於ImageView的舊drawable,以及如何?
Drawable oriDrawable = imageView.getDrawable()
// set callback to null
oriDrawable.setCallback(null);
// get the bitmap and recycle it
((BitmapDrawable)oriDrawable).getBitmap().recycle();
上面的代碼是否正確?什麼是最好的解決方案?
我確實有OutOfMemoryException,並已將其作爲URL的建議進行了修復。但是我確定如果我需要儘可能多地回收未使用的drawable,或者當我分配一個新的drawable時它會自動銷燬。 – Freewind
我會首先嚐試查看您是否因無法回收而出現問題,因爲這些問題將通過GC進行回收。只要確保沒有對drawable或bitmaps的額外引用使其符合GC標準。 –