转Android Canvas drawArc方法介绍

public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

  • oval :指定圆弧的外轮廓矩形区域。

  • startAngle: 圆弧起始角度,单位为度。

  • sweepAngle: 圆弧扫过的角度,顺时针方向,单位为度。

  • useCenter: 如果为True时,在绘制圆弧时将圆心包括在内,通常用来绘制扇形。

  • paint: 绘制圆弧的画板属性,如颜色,是否填充等。

本例演示了drawArc的四种不同用法,

1. 填充圆弧但不含圆心:

  1. mPaints[0] = new Paint();
  2. mPaints[0].setAntiAlias(true);
  3. mPaints[0].setStyle(Paint.Style.FILL);
  4. mPaints[0].setColor(0x88FF0000);
  5. mUseCenters[0] = false;

2. 填充圆弧带圆心(扇形)

  1. mPaints[1] = new Paint(mPaints[0]);
  2. mPaints[1].setColor(0x8800FF00);
  3. mUseCenters[1] = true;

3. 只绘圆周,不含圆心

  1. mPaints[2] = new Paint(mPaints[0]);
  2. mPaints[2].setStyle(Paint.Style.STROKE);
  3. mPaints[2].setStrokeWidth(4);
  4. mPaints[2].setColor(0x880000FF);
  5. mUseCenters[2] = false;

4. 只绘圆周,带圆心(扇形)

  1. mPaints[3] = new Paint(mPaints[2]);
  2. mPaints[3].setColor(0x88888888);
  3. mUseCenters[3] = true;

本例的onDraw

  1. protected void onDraw(Canvas canvas) {
  2.  canvas.drawColor(Color.WHITE);
  3.     
  4.  drawArcs(canvas, mBigOval, mUseCenters[mBigIndex],
  5.  mPaints[mBigIndex]);
  6.     
  7.  for (int i = 0; i < 4; i++) {
  8.  drawArcs(canvas, mOvals[i], mUseCenters[i], mPaints[i]);
  9.  }
  10.     
  11.  mSweep += SWEEP_INC;
  12.  if (mSweep > 360) {
  13.  mSweep -= 360;
  14.  mStart += START_INC;
  15.  if (mStart >= 360) {
  16.  mStart -= 360;
  17.  }
  18.  mBigIndex = (mBigIndex + 1) % mOvals.length;
  19.  }
  20.  invalidate();
  21.  }

同样onDraw之中调用invalidate(),会再触发onDraw,从而不停刷新显示,startAngle, sweepAngle周而复始,形成动画效果,最上的大图顺序显示drawArc的这四种用法:

Paint.Style.STROKE 表示当前只绘制图形的轮廓,而Paint.Style.FILL表示填充图形。

posted @ 2022-02-14 13:32  mao的博客  阅读(608)  评论(0编辑  收藏  举报