关于Bitmap.createBitmap创建新的Bitmap,

2022-03-24  本文已影响0人  伪装的狼

出现问题的代码如下:

val bitmap = BitmapFactory.decodeByteArray(nv21, 0, nv21.size)

val matrix = Matrix()
matrix.postRotate(Config.ROTATE_ANGLE)

val tempBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, false)

bitmap.recycle()

把bitmap回收之后,发现tempBitmap也被回收了,经过判断bitmap==tempBitmap的结果是true,然后查看源码,发现这么一段代码:

// check if we can just return our argument unchanged
if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() && height == source.getHeight() && (m == null || m.isIdentity())) {
    return source;
}

如果source也就是传进来的Bitmap是可变的,设置新Bitmap宽高指定位置和原来的一样,并且m也就是传进来的Matrix,判断isIdentity为true,那么就返回原Bitmap,所以问题就清楚了。

上一篇 下一篇

猜你喜欢

热点阅读