注:1.在Android2.2(API level 8)之前,当垃圾回收线程工作时,主线程会阻塞,这就造成了不好的用户体验,
所以在以后的版本中,出现了并发执行的垃圾回收线程,这意味着当一个Bitmap没有被引用时,就会被回收;2.在Android2.3.3(API level 10)之前,Bitmap的像素数据是存储在本地内存中,且与Bitmap本身是分开存储的,
(Bitmap存储在Dalvik虚拟机的堆内存中)。在Android3.0(API level 11)版本之后,两者都存储在虚拟机堆内存中。一、管理Android2.3.3之前版本设备的Bitmap存储
建议使用recycle()方法回收空间,应该在Bitmap对象不再被使用了调用该方法。如果已经recycle后尝试使用该Bitmap,会得到如下错误: "Canvas: trying to use a recycled bitmap".
private int mCacheRefCount = 0;
private int mDisplayRefCount = 0;
...
// Notify the drawable that the displayed state has changed.
// Keep a count to determine when the drawable is no longer displayed.
public void setIsDisplayed(boolean isDisplayed) {
synchronized (this) {
if (isDisplayed) {
mDisplayRefCount++;
mHasBeenDisplayed = true;
} else {
mDisplayRefCount--;
}
}
// Check to see if recycle() can be called.
checkState();
}
// Notify the drawable that the cache state has changed.
// Keep a count to determine when the drawable is no longer being cached.
public void setIsCached(boolean isCached) {
synchronized (this) {
if (isCached) {
mCacheRefCount++;
} else {
mCacheRefCount--;
}
}
// Check to see if recycle() can be called.
chec