Home » Android » Android Canvas.drawCircle in the center of the screen

Android Canvas.drawCircle in the center of the screen

Posted by: admin June 15, 2020 Leave a comment

Questions:

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 ?

How to&Answers:

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)

Answer:

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);

Answer:

Assuming you are extending the View class:

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