Android自訂UI執行個體

來源:互聯網
上載者:User

下面開始實現一個閃屏的效果,首先自訂類繼承於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 < 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中關於繪製平面2D圖形的類基本上都在android.graphics這個包中,比如常用的Paint、Path、Canvas、Rect、Bitmap、Color、Matrix、Point等等。這些雖然基礎,但卻是繪圖重要的類,所以先要掌握好。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.