如何绘制一个填充整个已更改视图的圆

时间:2015-06-26 13:57:41

标签: android ondraw

我正在尝试绘制一个圆圈,填充整个已更改的视图(对于任何设备)。 圆形将填充尽可能多的屏幕空间(不会变形其圆形形状)。 旋转屏幕方向将调整时钟视图。 lass Circles扩展了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);



  }
第一个圆圈,当手机垂直时根本不显示,但是当水平旋转时,可以看到圆圈的边缘。 nayone可以告诉我,我使用的onSizeChanged有什么问题。 ?

1 个答案:

答案 0 :(得分:0)

移动此行

 this.setMeasuredDimension( viewWidth, viewHeight);

这一行之后

Radius=Math.min(viewWidth, viewHeight);

看看这是否有效。