DrawBitmap: How can you set the coordinates and use the matrix?

I was looking at the documentation and the only drawBitmap() , which allows the Matrix as parameter not to accept any kind of screen coordinates for the parameter.

I use the following code to draw a bitmap with 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 to set the position and use the matrix?

Ideally, I would like to use a Rect destination:

 this.destRect = new Rect( x - spriteWidth / 2, y - spriteHeight / 2, x + spriteWidth / 2, y + spriteHeight /2 ); 

Edit:

The following sentence in the comment I tried:

  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. The bitmap is still drawn at 0,0

Edit2:

Here is a trick to rotate the image onto yourself using the matrix, and then place it on 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); 
+6
source share

Source: https://habr.com/ru/post/978353/


All Articles