Question

In a custom view, is there a way I can set the center of a drawable at a certain point on the canvas? (I don't want to deal with figuring out rectangular bounds for the drawable...)

Was it helpful?

Solution

When you make the call to draw the Bitmap, draw it at a point with it's left and top attributes set to the position minus half the image's width and height, respectively. That will center it at the point you want.

IE.

canvas.drawBitmap(bitmap, xPos - bitmap.getWidth()/2, yPos - bitmap.getHeight()/2, new Paint());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top