user1169775
user1169775

Reputation: 53

How to center an shape created through canvas?

I am using android java to create an application that uses the accelerometer, i have created a shape that does kinda move with the accelerometer but it stays in the top left hand corner and moves there, can anyone help me with how can i move it to the center of the screen and set bounds

Upvotes: 1

Views: 920

Answers (1)

Nav
Nav

Reputation: 10442

I am assuming you are using a canvas to draw that stuff if so canvas has a method called getwidth() and also getheight()

what you can do is when you are drawing it to the canvas set the location point x and y coordinates as

canvas.getWidth()/2
and canvas.getHeight()/2

something like this

canvas.drawBitmap(bitmap, canvas.getWidth()/2, canvas.getHeight()/2, null);

I hope you got the idea.

canvas.drawRect(left, top, right, bottom, paint)

use this function if you wanna draw a rectangle

Parameters: left The left side of the rectangle to be drawn

top The top side of the rectangle to be drawn

right The right side of the rectangle to be drawn

bottom The bottom side of the rectangle to be drawn

paint The paint used to draw the rect

Use the same logic in this method and you are golden ;)

Upvotes: 1

Related Questions