Canvas提供了drawArc来绘制圆弧。
public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
本例演示了drawArc的四种不同用法,
1. 填充圆弧但不含圆心:
mPaints[0] = new Paint(); mPaints[0].setAntiAlias(true); mPaints[0].setStyle(Paint.Style.FILL); mPaints[0].setColor(0x88FF0000); mUseCenters[0] = false;
2. 填充圆弧带圆心(扇形)
mPaints[1] = new Paint(mPaints[0]); mPaints[1].setColor(0x8800FF00); mUseCenters[1] = true;
3. 只绘圆周,不含圆心
mPaints[3] = new Paint(mPaints[2]); mPaints[3].setColor(0x88888888); mUseCenters[3] = true;
4. 只绘圆周,带圆心(扇形)
mPaints[3] = new Paint(mPaints[2]); mPaints[3].setColor(0x88888888); mUseCenters[3] = true;
本例的onDraw
protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); drawArcs(canvas, mBigOval, mUseCenters[mBigIndex], mPaints[mBigIndex]); for (int i = 0; i < 4; i++) { drawArcs(canvas, mOvals[i], mUseCenters[i], mPaints[i]); } mSweep += SWEEP_INC; if (mSweep > 360) { mSweep -= 360; mStart += START_INC; if (mStart >= 360) { mStart -= 360; } mBigIndex = (mBigIndex + 1) % mOvals.length; } invalidate(); }
同样onDraw之中调用invalidate(),会再触发onDraw,从而不停刷新显示,startAngle, sweepAngle周而复始,形成动画效果,最上的大图顺序显示drawArc的这四种用法:
Paint.Style.STROKE 表示当前只绘制图形的轮廓,而Paint.Style.FILL表示填充图形。