Android各版本间API的差异 - Bitmap

概述

Bitmap在API Level 1中就已经有了, 只不过随着SDK更新, Google对它的一些内外部接口/实现进行了一些优化或者调整, 主要是内存资源的管理方面. 


差异

  • 内存资源回收: Android 2.3(API Level 10)- 由于存储图像数据的Buffer分配在Native内存中, 并且只能通过 recycle( ) 来显式释放Buffer , 而3.0+中,该Buffer则分配在Dalvik Heap中, 因此 recycle( ) 通常当成兼容向下版本的调用. p.s. 该Buffer可在Android源码Bitmap.java中找到.
  • On Android Android 2.2 (API level 8) and lower, when garbage collection occurs, your app's threads get stopped. This causes a lag that can degrade performance. Android 2.3 adds concurrent garbage collection, which means that the memory is reclaimed soon after a bitmap is no longer referenced. 
  • On Android 2.3.3 (API level 10) and lower, the backing pixel data for a bitmap is stored in native memory. It is separate from the bitmap itself, which is stored in the Dalvik heap. The pixel data in native memory is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash. As of Android 3.0 (API level 11), the pixel data is stored on the Dalvik heap along with the associated bitmap.
  • 内存重利用: 3.0+允许开发者将已存在的Bitmap.Buffer分配给新的Bitmap来使用, 不过Bitmap之间的大小要严格相同, 而4.4(API Level 19)+则改变这一状况.
  • BitmapFactory.Options: 详见SDK API Reference.


扩展阅读

上述都只是针对单个Bitmap的内存管理, 但是在开发中, 难免会需要管理大量的Bitmap, 那应该怎么办?

在API Level 12中, 可以通过LruCache来缓存大量的内存Bitmap. 除此之外, 在Android源码中, 可以找到, 一个名为DiskLruCache的类, 它可以文件形式缓存Bitmap.


参考资料

Google Taining

转载于:https://my.oschina.net/erehmii/blog/193546

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值