管理Bitmap的存储空间

注: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值