2016-02-28 57 views
0

Sample Image如何縮放畫布上繪製的位圖,同時保存用矩陣指定的旋轉和平移

大家好。我是Android的使用矩陣的圖像處理的新手。我正在研究在設備屏幕上顯示位圖#1的應用程序。

位圖#1真的很大,約爲2592 X 1456,但縮小到適合設備的屏幕尺寸僅用於顯示目的。

然後,我使用矩陣(使用旋轉,縮放,平移)在位圖#1畫布上繪製了雙脣位圖(#2),如上圖所示。

正是我想實現的是保存最終位圖的副本,向後縮放到原始大小(2592 x 1456)。

我試圖通過縮放位圖#1矩陣來實現它。

這是我到目前爲止已經試過:

// adjust the matrix to the original image size 
    // new matrix big 
    Matrix newMatrix = new Matrix(); 
    // copy matrix from small matrix 
    newMatrix = matrix; 
    RectF src = new RectF(0,0,backgroundImage.getWidth(), backgroundImage.getHeight()); 
    RectF dst = new RectF(0,0, origBackground.getWidth(), origBackground.getHeight()); 
    newMatrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER); 
    canvas.drawBitmap(bitmap, newMatrix, paint); 

我的問題是,在生成位圖#1,嘴脣位圖(#2)被放置在x = 0和Y = 0而不是在所需的座標,缺少指定的旋轉。

+0

http://developer.android.com/guide/practices/screens_support.html#scaling – xAF

+0

@xAF嗨。這是在畫布上繪製的。我沒有使用任何自動調整屏幕的小部件。我正在使用矩陣來做到這一點。 –

+0

請檢查我的建議編輯(希望我理解你的問題) – xAF

回答

0

爲了擴展的目的,更好的選擇是使用Canvas自己的save()restore()方法。

參見this answer

爲了您的目的,使用translate(x,y)在特定座標處繪製並以rotate(deg,x,y)繪製以固定在特定座標處的特定旋轉角度繪製。

希望這將有助於解決您的問題。

+0

Hi @xAF是正確的,但我希望它在Matrix解決方案中。所以我把你的答案轉換成矩陣解決方案。以上是我的答案。謝謝 –

0

經過一小時的嘗試。我終於得到了答案。以下是我一直使用的源代碼。感謝@xAF。

乾杯,

public Bitmap saveBitmap() 
    { 
    Bitmap bm = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), Bitmap.Config.ARGB_8888); 
    mCanvas = new Canvas(bm); 
    // draw the background photo with its original size 
    mCanvas.drawBitmap(bitmap1, 0, 0, null); 

    // put the lips and adjust the matrix to the original image size 
    Matrix newMatrix = new Matrix(); // new big matrix 
    // copy the screen small matrix (with rotate, scale, translation) 
    newMatrix.set(matrix); 
    float scaleWidth = ((float) bitmap1.getWidth())/bitmap2.getWidth(); 
    float scaleHeight = ((float) bitmap1.getHeight())/bitmap2.getHeight(); 

    newMatrix.postScale(scaleWidth, scaleHeight, 0, 0); 
    mCanvas.drawBitmap(bitmapLips, newMatrix, paint); 

    return bm; 
}