hsz
hsz

Reputation: 152206

Android Canvas.drawCircle in the center of the screen

I am using Canvas.drawCircle to draw a circle in Android laout.

Method gets 3 parameters - first two are position - x and y.

Is it possible to skip hardcoded position of the circle and draw it centered ?

Upvotes: 10

Views: 18519

Answers (3)

Kuffs
Kuffs

Reputation: 35661

Assuming you are extending the View class:

int CentreX = (this.getWidth() / 2);
int CentreY = (this.getHeight() / 2);

Upvotes: 3

Narcís Calvet
Narcís Calvet

Reputation: 7452

You can paint a circle centered to the screen like this:

Display disp = ((WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
canvas.drawCircle(disp.getWidth()/2, disp.getHeight()/2, radius, paint);

Upvotes: 3

variant-45
variant-45

Reputation: 391

Following code can be used to get the width and height of the screen.

int width = this.getWidth(); 
int height = this.getHeight(); 

To draw circle in the middle of screen you can call :

Canvas.drawCircle(width/2, height/2)

Upvotes: 18

Related Questions