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();
	}
}
Android自定义UI实例_第1张图片
下面对上一篇文章的例子增强一下:

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 < 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();
        }
    }
}
效果如下:

Android自定义UI实例_第2张图片

android中关于绘制平面2D图形的类基本上都在android.graphics这个包中,比如常用的Paint、Path、Canvas、Rect、Bitmap、Color、Matrix、Point等等。这些虽然基础,但却是绘图重要的类,所以先要掌握好。

你可能感兴趣的:(Android自定义UI实例)