Android自定義UI實例

下面開始實現一個閃屏的效果,首先自定義類繼承於View,然後重寫onDraw方法,之前使用onDraw方法可以繪圖,但是隻是繪制一次,那如何實現循環呢。很容易想到使用invalidate()這個方法,因為使用這個方法它就會調用onDraw方法,這樣就形成瞭一個死循環,不斷刷新繪制界面。當然還有一個postInvalidate().方法,它與invalidate()的區別在於它用於非UI的線程,invalidate()必須在UI線程使用。所以,代碼可以這樣寫:

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.view.View;

class RenderView extends View {
	Random rand = new Random();

	public RenderView(Context context) {
		super(context);
	}

	protected void onDraw(Canvas canvas) {
		canvas.drawRGB(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
		invalidate();
	}
}

下面對上一篇文章的例子增強一下:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;

public class Test extends GraphicsActivity {

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

    private static class SampleView extends View {
    	//畫筆 
        private Paint[] mPaints;
        //畫筆
        private Paint mFramePaint;
        //是否有圓心
        private boolean[] mUseCenters;
        //矩形
        private RectF[] mOvals;
        //矩形
        private RectF mBigOval;
        //開始弧度
        private float mStart;
        //增量弧度
        private float mSweep;
        //索引
        private int mBigIndex;
        //掃描增量
        private static final float SWEEP_INC = 2;
        private static final float START_INC = 15;

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

            mPaints = new Paint[4];
            mUseCenters = new boolean[4];
            mOvals = new RectF[4];

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

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

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

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

            mBigOval = new RectF(40, 10, 280, 250);

            mOvals[0] = new RectF( 10, 270,  70, 330);
            mOvals[1] = new RectF( 90, 270, 150, 330);
            mOvals[2] = new RectF(170, 270, 230, 330);
            mOvals[3] = new RectF(250, 270, 310, 330);

            mFramePaint = new Paint();
            mFramePaint.setAntiAlias(true);
            mFramePaint.setStyle(Paint.Style.STROKE);
            mFramePaint.setStrokeWidth(0);
        }
        /**
         * @category 
         * @param canvas
         * @param oval
         * @param useCenter
         * @param paint
         */
        private void drawArcs(Canvas canvas, RectF oval, boolean useCenter,
                              Paint paint) {
        	//畫矩形
            canvas.drawRect(oval, mFramePaint);
            //畫弧形
            canvas.drawArc(oval, mStart, mSweep, useCenter, paint);
        }

        @Override protected void onDraw(Canvas canvas) {
        	//設置背景色
            canvas.drawColor(Color.WHITE);
            //畫大矩形
            drawArcs(canvas, mBigOval, mUseCenters[mBigIndex],
                     mPaints[mBigIndex]);
            //畫四個小矩形
            for (int i = 0; i  360) {
                mSweep -= 360;
                mStart += START_INC;
                if (mStart >= 360) {
                    mStart -= 360;
                }
                //變換
                mBigIndex = (mBigIndex + 1) % mOvals.length;
            }
            //刷新
            invalidate();
        }
    }
}

效果如下:

android中關於繪制平面2D圖形的類基本上都在android.graphics這個包中,比如常用的Paint、Path、Canvas、Rect、Bitmap、Color、Matrix、Point等等。這些雖然基礎,但卻是繪圖重要的類,所以先要掌握好。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *