Android Canvas.drawCircle in the center of the screen

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

The method receives 3 parameters: the first two positions are x and y .

Is it possible to skip the hard-coded position of the circle and draw it in the center?

+7
source share
3 answers

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

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

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

 Canvas.drawCircle(width/2, height/2) 
+16
source

Assuming you extend the view class:

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

You can draw a circle centered on the screen as follows:

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

All Articles