learner
learner

Reputation: 11790

How do I find center of drawable for android canvas

I have a drawable resource that I need to draw on a canvas. I need to have the drawable centered at point (x,y). Right now if I do canvas.drawBitmap(mBitmap, x, y, mPaint), (x,y) represent (left,top). Does anyone know how I may get the center of my drawable so I can place it on the canvas correctly?

Upvotes: 3

Views: 1746

Answers (2)

Boldijar Paul
Boldijar Paul

Reputation: 5495

Get the width and height of the canvas. Now, you need to draw it on width/2 and height/2.

To be more centered, you can subtract from width/2 half of the bitmap width, and from height/2 half of the bitmap height.

canvas.drawBitmap(mBitmap, width/2-mBitmap.getWidth()/2, height/2-mBitmap.getHeight()/2, mPaint);

Upvotes: 4

Gopal Gopi
Gopal Gopi

Reputation: 11131

A Simple math...

    int centerX = drawable.getIntrinsicWidth() / 2;
    int centerY = drawable.getIntrinsicHeight() / 2;

Upvotes: 3

Related Questions