I've been looking at the documentation and the only drawBitmap()
that allows a Matrix
as parameter doesn't take any kind of screen coordinates by parameter.
I'm using the following code to draw my bitmap with a matrix rotation:
Matrix matrix = new Matrix();
matrix.setRotate(rotation,bitmap.getWidth()/2,bitmap.getHeight()/2);
canvas.drawBitmap(bitmap, matrix, null);
Which of course draws my bitmap at 0,0
. How can I set the position and use the matrix?
Ideally I'd like to use the destination Rect:
this.destRect = new Rect(
x - spriteWidth / 2,
y - spriteHeight / 2,
x + spriteWidth / 2,
y + spriteHeight /2
);
Edit:
Following suggestin in comment I tried this:
Matrix matrix = new Matrix();
matrix.setTranslate(x, y);
matrix.setRotate(rotation,bitmap.getWidth()/2,bitmap.getHeight()/2);
canvas.drawBitmap(bitmap, matrix, null);
but to no avail. Bitmap is still drawn at 0,0
Edit2:
Here's the trick to rotate an image on itself using a matrix and then place it at x,y
:
Matrix matrix = new Matrix();
matrix.reset();
matrix.postTranslate(-bitmap.getWidth() / 2, -bitmap.getHeight() / 2); // Centers image
matrix.postRotate(rotation);
matrix.postTranslate(x, y);
canvas.drawBitmap(bitmap, matrix, null);