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

你可能感兴趣的:(android,api,bitmap,sdk,差异)