Android - color a bitmap at a specific location on the canvas

Well, maybe something is missing for me, but I'm stuck for hours. I am creating an application in which the user draws a dimension line above the image. Now I also want to draw some selection points that show that the line is selected. These points are a specific bitmap that should be at the end of the line (after the arrow) and rotated in accordance with the arrow. I created a DrawSelectionPoint class that extends the view, and I can rotate the bitmap like this:

selectionPoint = BitmapFactory.decodeResource(context.getResources(), R.drawable.selectionpoint); Matrix matrix = new Matrix(); matrix.postRotate((float)Math.toDegrees(angle)); canvas.drawBitmap(selectionPoint, matrix, null); 

(where the angle is the angle of the line), so my bitmap rotates the way I want it, but it is painted at the point 0,0 (top left of the screen).

If I use something like

 canvas.save(); canvas.rotate(); canvas.drawBitmap(selectionPoint, x, y, null); canvas.restore(); 

then it’s too difficult for me to draw a bitmap in the exact place that I want (since I draw on a rotated canvas, which I then turn back). I tried some transformations of Euclidean rotation, but I had no luck.

Is there a way to apply the rotation of the matrix, as well as indicate the points at which I need a bitmap? Thank you in advance!

+6
source share
1 answer

Suppose you want to draw a bitmap where the center of the bitmap will be in the canvas coordinates (px, py). Have a member variable

 Matrix matrix = new Matrix(); 

and in your onDraw:

 matrix.reset(); matrix.postTranslate(-bitmap.getWidth() / 2, -bitmap.getHeight() / 2); // Centers image matrix.postRotate(angle); matrix.postTranslate(px, py); canvas.drawBitmap(bitmap, matrix, null); 
+18
source

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


All Articles