android - Draw Circle at Center of Canvas -


i'm getting basic drawing android. i'm starting off few simple shapes i'm having few issues. i'd draw circle @ center of canvas. looked @ few examples can't seem make work. think it's because don't understand variables go where.

could please explain proper way draw circle @ center of screen. here code:

public class circle extends view{  int width = this.getwidth(); int height = this.getheight();  public circle(context context) {     super(context);     setfocusable(true);  } protected void ondraw(canvas canvas){     canvas.drawcolor(color.cyan);     paint paint = new paint();     paint.setstyle(paint.style.fill);     //canvas.drawcircle(100, 100, 50, paint);     canvas.drawcircle(width/2, height/2, 100, paint);      display disp = ((windowmanager)this.getcontext().getsystemservice(context.window_service)).getdefaultdisplay();      float radius = 0;//radius caused error initialized variable      canvas.drawcircle(disp.getwidth()/2, disp.getheight()/2, radius, paint);  } 

}

width , height of view have not been yet initialized when getwidth() , getheight() called, use getwidth() , getheight() in ondraw:

canvas.drawcircle(getwidth()/2, getheight()/2, 100, paint); 

you can override onsizechanged , view width , height.

ps: not create in ondraw, create paint object in constructor.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -