天天看點

Android Api Demos登頂之路(六十七)Graphics-->Arcs

Canvas提供了drawArc來繪制圓弧。

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

oval :指定圓弧的外輪廓矩形區域。

startAngle: 圓弧起始角度,機關為度。

sweepAngle: 圓弧掃過的角度,順時針方向,機關為度。

useCenter: 如果為True時,在繪制圓弧時将圓心包括在内,通常用來繪制扇形。

paint: 繪制圓弧的畫闆屬性,如顔色,是否填充等。

本例示範了drawArc的四種不同用法:

1.填充圓弧但不含圓心、

2.填圓弧含圓心(扇形)

3.繪制圓周但不含圓心

4.繪制圓周含圓心(扇形)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    private class SampleView extends View{
        //定義繪制四種圓弧時使用的不同畫筆
        private Paint[] mPaints;
        //定義繪制邊框時的畫筆
        private Paint mFramePaint;
        //是否包含圓心
        private boolean[] mUseCenter;
        //定義繪制四種圓弧時的邊框
        private RectF mOvals[];
        //定義繪制大圓弧時的邊框
        private RectF mBigOval;
        //定義起始的角度和圓弧掃過的角度
        private float mStart;
        private float mSweep;
        private int mBigIndes;

        //定義角度和掃過的角度的變化幅度
        private static final float START_INC=;
        private static final float SWEEP_INC=;
        public SampleView(Context context) {
            super(context);

            mPaints=new Paint[];
            mUseCenter=new boolean[];
            mOvals=new RectF[];

            //定義第一種畫筆,用88%透明度的紅色填充,不包含圓心
            mPaints[]=new Paint();
            mPaints[].setAntiAlias(true);
            mPaints[].setStyle(Paint.Style.FILL);
            mPaints[].setColor();
            mUseCenter[]=false;
            //定義第二種畫筆,用88%透明度的綠色填充,包含圓心
            mPaints[]=new Paint(mPaints[]);
            mPaints[].setColor();
            mUseCenter[]=true;
            //定義第三種畫筆,用88%透明度的藍色繪制圓周,不包含圓心
            mPaints[]=new Paint(mPaints[]);
            mPaints[].setStyle(Paint.Style.STROKE);
            mPaints[].setStrokeWidth();
            mPaints[].setColor();
            mUseCenter[]=false;
            //定義第四種畫筆,用88%透明度的灰色繪制圓周,包含圓心
            mPaints[]=new Paint(mPaints[]);
            mPaints[].setColor();
            mUseCenter[]=true;

            //定義繪制包含圓弧的邊框
            //大圓弧的邊框
            mBigOval=new RectF(, , , );
            //四個小圓弧邊框
            mOvals[]=new RectF(, , , );
            mOvals[]=new RectF(, , , );
            mOvals[]=new RectF(, , , );
            mOvals[]=new RectF(, , , );

            //定義繪制邊框的畫筆
            mFramePaint=new Paint();
            mFramePaint.setAntiAlias(true);
            mFramePaint.setStyle(Paint.Style.STROKE);
            //設定空心線寬
            mFramePaint.setStrokeWidth();
        }

        //重寫ondraw方法繪制圖形
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.WHITE);
            //繪制大的圓弧和邊框
            drawArcs(canvas,mBigOval,mUseCenter[mBigIndes],mPaints[mBigIndes]);
            //繪制小的圓弧和邊框
            for(int i=;i<mOvals.length;i++){
                drawArcs(canvas, mOvals[i], mUseCenter[i], mPaints[i]);
            }

            mSweep+=SWEEP_INC;
            //當繪制完整個圓形後,在新的位置重新開始繪制另一個圓弧
            if(mSweep>){
                mSweep-=;
                mStart+=START_INC;
                if(mStart>){
                    mStart-=;
                }
                mBigIndes=(mBigIndes+)%mOvals.length;
            }
            invalidate();
        }

        //繪制邊框的圓弧
        private void drawArcs(Canvas canvas, RectF oval, boolean useCenter,
                Paint paint) {
            //繪制邊框
            canvas.drawRect(oval, mFramePaint);
            //繪制圓弧
            canvas.drawArc(oval, mStart, mSweep, useCenter, paint);
        }

    }
}