inBitmap是在BitmapFactory中的内部类Options的一个变量,简单而言,使用该变量能够复用旧的Bitmap的内存而不用从新分配以及销毁旧Bitmap,进而改善运行效率。html
关于Bitmap的相关知识能够查看我写的Android中Bitmap的深刻探讨总结。java
inBitmap变量是在Android 3.0+版本加入到系统源码当中,也就意味着inBitmap参数只有在Android 3.0+版本及以上可以正常使用,当你的app版本低于3.0的时候,仍是老老实实的使用bitmap.recycle()进行Bitmap的回收操做;在Android 3.0+以上根据系统版本的不一样,使用inBitmap的规则也不相同,具体区分以下:android
除上述规则以外,旧Bitmap必须是mutable的,这点也很好理解,若是一个Bitmap不支持修改,那么其内存天然也重用不了。Ok,关于inBitmap的知识点理论上也就那么多。Google为了帮助咱们更好的管理Bitmap,也出了一个视频,视频地址以下:缓存
https://www.youtube.com/watch?v=_ioFW3cyRV0&index=17&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCEapp
而且附带上了一个使用inBitmap的Demo:框架
https://developer.android.com/topic/performance/graphics/manage-memory.html#javaide
下面贴视频中两张图更好的帮助一下理解:ui
使用inBitmap以前:.net
使用inBitmap以后:设计
针对上述的理解,这里有一个疑问须要去确认一下:
解决这个疑问能够查看Google官方的inBitmap Demo来回答问题:
/** * candidate:旧的图片,targetOptions:新的图片的Options */ private fun canUseForInBitmap(candidate: Bitmap, targetOptions: BitmapFactory.Options): Boolean { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // From Android 4.4 (KitKat) onward we can re-use if the byte size of // the new bitmap is smaller than the reusable bitmap candidate // allocation byte count. val width: Int = targetOptions.outWidth / targetOptions.inSampleSize val height: Int = targetOptions.outHeight / targetOptions.inSampleSize val byteCount: Int = width * height * getBytesPerPixel(candidate.config) byteCount <= candidate.allocationByteCount } else { // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1 candidate.width == targetOptions.outWidth && candidate.height == targetOptions.outHeight && targetOptions.inSampleSize == 1 } } /** * A helper function to return the byte usage per pixel of a bitmap based on its configuration. */ private fun getBytesPerPixel(config: Bitmap.Config): Int { return when (config) { Bitmap.Config.ARGB_8888 -> 4 Bitmap.Config.RGB_565, Bitmap.Config.ARGB_4444 -> 2 Bitmap.Config.ALPHA_8 -> 1 else -> 1 } }
这里能够看出,只要新的Bitmap的内存小于旧Bitmap的内存大小,便可进行复用的操做,那么跟解码模式没有必然的联系。
关于inBitmap的使用,能够根据谷歌的官方例子进行设计和开发,不过维护起来须要必定的工做量。固然在市面上成熟的图片框架中,如Glide内部也使用了inBitmap做为缓存复用的一种方式。总而言之,根据项目以及业务来选择实现方式便可,没必要过度纠结。