2012-09-20 36 views
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()以及我的代碼出了什麼問題?

+2

我猜想,當他的位圖被矩陣轉換時總是會創建一個新的對象。如果原始文件是不可變的,它如何轉換原始對象? –

+0

我第二個丹尼爾。這就是我正在從定義中讀出的內容。只有在沒有變化的情況下,即在呼叫不必要的情況下,同樣的情況也是如此。 – Fildor

+0

你有沒有使用'isMutable()'方法檢查'OrientedPhoto'以確定它是否可變? –

回答

5

似乎它真的只是不清楚這種方法的文檔。我發現這個代碼BitmapcrateBitmap()方法:

// 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; 
} 

這意味着,源位圖僅在回國的情況下回來,如果它是不可變並且無需轉換。

相關問題