2015-02-24 25 views
0

我想在畫布,Android中使用drawBitmap()將兩個位圖相鄰放置。如何在Android canvas中使用drawBitmap來定位位圖

我的onDraw()功能。

protected void onDraw(Canvas canvas) { 

    if (currentState == openedState) { 
      fruit1Bitmap = ApplicationServices.textureManager.bitmap[fruitId[0]]; 
      fruit2Bitmap = ApplicationServices.textureManager.bitmap[fruitId[1]]; 
      fruit3Bitmap = ApplicationServices.textureManager.bitmap[fruitId[2]]; 
      src.set(0, 0, fruit1Bitmap.getWidth(), fruit1Bitmap.getHeight()); 
      dst.set(0,0, this.getWidth()/2, this.getHeight()/2); 
      src1.set(0, 0, fruit2Bitmap.getWidth(), fruit2Bitmap.getHeight()); 
      dst1.set(fruit1Bitmap.getWidth() , 0, this.getWidth()/2, this.getHeight()/2); 

      canvas.drawBitmap(fruit1Bitmap, src, dst, null); 
      canvas.drawBitmap(fruit2Bitmap, src1, dst1, null); 
    } 
} 

它是在類public class Dhakkan extends ImageButton

當前結果 enter image description here

我想要得到它,以顯示彼此相鄰兩種水果。那麼如何將它們放置在ImageButton之內。

回答

1
  • 你計算你的第二個目標矩形錯

而不是

dst1.set(fruit1Bitmap.getWidth() , 0, this.getWidth()/2, this.getHeight()/2); 

它應該是這樣的:

dst1.set(fruit1Bitmap.getWidth(), 
    0, 
    fruit1Bitmap.getWidth() + fruit2Bitmap.getWidth(), 
    this.getHeight()/2); 

當心你的右側座標。這會在第一個果實旁邊畫出第二個果實,如果它太大,可能會剪切。如果您想在圖像按鈕的前半部分中繪製兩個水果,則修復第一個水果的目標矩形dst的座標。你也可以考慮Kim提出的方法。

+1

是的!感謝它的工作! – 2015-02-25 05:32:34

0

如何讀取文檔:

drawBitmap(Bitmap bitmap, float left, float top, Paint paint) 

在(X,Y)畫出指定的位圖,其頂部/左上角,使用指定的塗料,通過當前矩陣變換。

相關問題