2011-03-24 56 views
1

我想旋轉,然後使用android.graphics.Matrix縮放圖像。代碼如下:在Android中使用矩陣旋轉和縮放圖像

int width = bmp1.getWidth(); 
    int height = bmp1.getHeight(); 
    int smallerVal = (height > width) ? width : height; 
    int n = 0; 
    while (smallerVal > 1) 
    { 
     smallerVal = smallerVal >> 1; 
     n++; 
    } 

    int pow2 = (int) Math.pow(2, n); 
    float scaleWidth = ((float) AppBase.width)/(float)pow2; 
    float scaleHeight = ((float) AppBase.height)/(float)pow2; 

    Matrix matrix = new Matrix(); 
    matrix.postRotate(90); 
    matrix.postScale(scaleWidth, scaleHeight); 
    Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, 0, pow2, pow2, matrix, true); 

pow2是爲了應用紋理而得到平方冪的圖像。 bmp2原來有-1作爲高度和寬度,即一個無效的圖像。我錯過了什麼嗎?

感謝,
Rajath

回答

1

我用我的問題的解決方案是下面,萬一有人需要它的未來。

  int pow2 = getSquareVal(bmp1); 
      int width = bmp1.getWidth(); 
      int height = bmp1.getHeight(); 
      float scaleWidth = ((float) pow2)/(float)width; 
      float scaleHeight = ((float) pow2)/(float)height; 

      Matrix matrix = new Matrix(); 
      matrix.reset(); 

      matrix.postScale(scaleWidth, scaleHeight); 
      if (width > height) 
       matrix.postRotate(90); 

      bmp2 = Bitmap.createBitmap(bmp1, 0, 0, width, height, matrix, true); 
+0

你的代碼對我來說真的很有價值。我想要在seekbar上旋轉圖像,然後縮放它以適應imageview的高度寬度。你有什麼建議嗎? – 2013-09-19 12:56:59