2
根据文档Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
方法:为什么Bitmap.createBitmap()返回与源位图不同的可变位图?
从源位图, 由可选矩阵变换的子集返回一个不可变位图。新的位图可能是与源相同的对象,或者可能已经创建了副本。用与原始位图相同的密度初始化了 。如果 源位图是不可变的,并且请求的子集是与源位图本身相同的 ,则返回源位图 并且不创建新的位图。
我已经得到了应用取向存在的位图的方法:
private Bitmap getOrientedPhoto(Bitmap bitmap, int orientation) {
int rotate = 0;
switch (orientation) {
case ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ORIENTATION_ROTATE_90:
rotate = 90;
break;
default:
return bitmap;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
我从这里呼唤它:
Bitmap tmpPhoto = BitmapFactory.decodeFile(inputPhotoFile.getAbsolutePath(), tmpOpts);
Bitmap orientedPhoto = getOrientedPhoto(tmpPhoto, orientation);
我检查了tmpPhoto
是不可改变的,但getOrientedPhoto()
仍然返回作为tmpPhoto的副本的可变图像。有谁知道如何在不创建新的位图对象的情况下使用Bitmap.createBitmap()
以及我的代码出了什么问题?
我猜想,当他的位图被矩阵转换时总是会创建一个新的对象。如果原始文件是不可变的,它如何转换原始对象? –
我第二个丹尼尔。这就是我正在从定义中读出的内容。只有在没有变化的情况下,即在呼叫不必要的情况下,同样的情况也是如此。 – Fildor
你有没有使用'isMutable()'方法检查'OrientedPhoto'以确定它是否可变? –