Draw an image in the center of another rectangle using canvas

In my opinion, I have a big rectangle, the rectangle can move. When the rectangle moves to some place, I want to draw an image in the center of a large rectangle. My question is that I cannot put the center of the image in the center of the rectangle. I used:

canvas.drawBitmap(rotatedBitmap, matrix, paint) canvas.drawBitmap(rotatedBitmap, left, top, paint) 

but I can’t find canvas.drawBitmap (rotatedBitmap, centerX, centerY, paint), so I want to use a matrix, but the matrix also moves the image to the left and top, and not from the center, can you give a hint to draw a pic in the center of the rectangle?

+4
source share
1 answer

Try using the borders of the rectangle as a reference point, and then use something like:

imageStartX = (rectStartX + (rectWidth / 2)) - (imageWidth / 2);

imageStartY = (rectStartY + (rectHeight / 2)) - (imageHeight / 2);

+6
source

All Articles