2015-02-24 133 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)画出指定的位图,其顶部/左上角,使用指定的涂料,通过当前矩阵变换。