这个问题与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标准。 –