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'以確定它是否可變? –