Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 325
hsz Avatar asked Nov 15 '11 12:11

hsz


2 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)
like image 121
variant-45 Avatar answered Nov 15 '22 20:11

variant-45


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);
like image 39
Narcís Calvet Avatar answered Nov 15 '22 21:11

Narcís Calvet