Reputation: 73
I want to position two bitmaps next to each other using drawBitmap()
in Canvas, Android.
My onDraw()
function.
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);
}
}
It is inside the class public class Dhakkan extends ImageButton
.
Current Result
I want to get it to show two fruits next to each other. So how do I position them within the ImageButton
.
Upvotes: 0
Views: 9224
Reputation: 3562
Instead of
dst1.set(fruit1Bitmap.getWidth() , 0, this.getWidth()/2, this.getHeight()/2);
It should be something like:
dst1.set(fruit1Bitmap.getWidth(),
0,
fruit1Bitmap.getWidth() + fruit2Bitmap.getWidth(),
this.getHeight()/2);
Watch out for your right coordinate. This will draw the second fruit next to the first, possibly cropping it if it's too large. If you want to draw both fruits in the first half of the image button instead, then fix the coordinates of dst
, the destination rectangle of the first fruit. You might also consider the method suggested by Kim.
Upvotes: 1
Reputation: 5728
How about reading the docs:
drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.
Upvotes: 0