draw an image in the center of another rectangle u

2019-05-04 17:41发布

问题:

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

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

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

回答1:

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

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

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