How to draw a circle which fills the entire changed view

64 views Asked by At

i am trying to draw a circle which fills the entire changed view (for any device). Circle will fill as much screen space as possible (without deforming its circular shape). Rotating the screen orientation will adjust the clock view. lass Circles extends View

{
    private int x = 0; 
    private int y = 0; 
    private final int rsec=240;
    private final int rmin=200;
    private final int rhrs=150;
    private Date date;

    int viewWidth = 0;
    int viewHeight = 0;
    int Radius = 0;

public Circles(Context context)
  {
      super(context);

 }

  public void onDraw(final Canvas canvas) 
  {
      super.onDraw(canvas);

       String msg = "width: " + viewWidth + "height: " + viewHeight;
       System.out.println(msg);

       x = viewWidth / 2 ; 
       y = viewHeight / 2 ;


      //2 Circels
      Paint p1 = new Paint();
      p1.setColor(Color.BLUE);
      p1.setStyle(Style.STROKE);
      Paint p2 = new Paint();
      p2.setColor(Color.RED);
      p2.setStyle(Style.FILL);
      canvas.drawCircle(x, y , Radius, p1);
      canvas.drawCircle(x , y , 20, p2);


@Override
  protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
       super.onSizeChanged(xNew, yNew, xOld, yOld);

          viewWidth = xNew;
          viewHeight = yNew;
          /*
          these viewWidth and viewHeight variables
          are the global int variables
          that were declared above
          */
          this.setMeasuredDimension( viewWidth, viewHeight);

          Radius=Math.min(viewWidth, viewHeight);



  }

the 1st circle , is not display at all when the phone is vertical , but when rotate him horizontal , the edges of the circke can be seen . is nayone can tell me whats wrong with the onSizeChanged i used. ?

1

There are 1 answers

2
Trinadh venna On

move this line

 this.setMeasuredDimension( viewWidth, viewHeight);

after this line

Radius=Math.min(viewWidth, viewHeight);

and see if this works.