关于Bitmap中的inBitmap变量的学习与使用

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

inBitmap是在BitmapFactory中的内部类Options的一个变量,简单而言,使用该变量可以复用旧的Bitmap的内存而不用重新分配以及销毁旧Bitmap,进而改善运行效率。

关于Bitmap的相关知识可以查看我写的Android中Bitmap的深入探讨总结。

inBitmap知识点

inBitmap变量是在Android 3.0+版本加入到系统源码当中,也就意味着inBitmap参数只有在Android 3.0+版本及以上能够正常使用,当你的app版本低于3.0的时候,还是老老实实的使用bitmap.recycle()进行Bitmap的回收操作;在Android 3.0+以上根据系统版本的不同,使用inBitmap的规则也不相同,具体区分如下:

  • 4.4之前的版本inBitmap只能够重用相同大小的Bitmap内存区域。简单而言,被重用的Bitmap需要与新的Bitmap规格完全一致,否则不能重用。
  • 4.4之后的版本系统不再限制旧Bitmap与新Bitmap的大小,只要保证旧Bitmap的大小是大于等于新Bitmap大小即可。

除上述规则之外,旧Bitmap必须是mutable的,这点也很好理解,如果一个Bitmap不支持修改,那么其内存自然也重用不了。Ok,关于inBitmap的知识点理论上也就那么多。Google为了帮助我们更好的管理Bitmap,也出了一个视频,视频地址如下:

https://www.youtube.com/watch?v=_ioFW3cyRV0&index=17&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE

并且附带上了一个使用inBitmap的Demo:

https://developer.android.com/topic/performance/graphics/manage-memory.html#java

下面贴视频中两张图更好的帮助一下理解:

使用inBitmap之前:

关于Bitmap中的inBitmap变量的学习与使用_第1张图片

使用inBitmap之后:

关于Bitmap中的inBitmap变量的学习与使用_第2张图片

inBitmap的疑问

针对上述的理解,这里有一个疑问需要去确认一下:

  • inBitmap的解码模式跟新Bitmap的不同是否能够重用成功

解决这个疑问可以查看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的使用

关于inBitmap的使用,可以根据谷歌的官方例子进行设计和开发,不过维护起来需要一定的工作量。当然在市面上成熟的图片框架中,如Glide内部也使用了inBitmap作为缓存复用的一种方式。总而言之,根据项目以及业务来选择实现方式即可,不必过分纠结。

转载于:https://my.oschina.net/u/3863980/blog/3019921

你可能感兴趣的:(关于Bitmap中的inBitmap变量的学习与使用)