Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂 View(三):圓環交替等待效果實作

編輯:關於 Android 程式設計

一位朋友提出了這樣的需求(如下圖),我覺得用自訂 View 來實現相當合適,於是動手做了一下,順便與大家分享。多練習自訂 View 總沒壞處,如果你看過前兩篇,這篇必定輕鬆上手。

分析後,這個效果大約有幾個屬性:兩個顏色、一個速度,以及圓環的寬度。還是我們自訂 View 的標準步驟:

1、自訂 View 的屬性

2、在 View 的構造方法中取得自訂屬性

[3、重寫 onMeasure]

4、重寫 onDraw

一、自訂屬性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomProgressBar">
    </declare-styleable>
</resources>

二、在構造方法中取得自訂屬性

/** 第一圈的顏色 */
private int mFirstColor;
/** 第二圈的顏色 */
private int mSecondColor;
/** 圈的寬度 */
private int mCircleWidth;
/** 畫筆 */
private Paint mPaint;
/** 當前進度 */
private int mProgress;
/** 速度 */
private int mSpeed;
/** 是否應該開始下一個 */
private boolean isNext = false;

public CustomProgressBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomProgressBar(Context context) {
    this(context, null);
}

/**
 * 必要的初始化,獲得一些自定義的值
 */
public CustomProgressBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs, R.styleable.CustomProgressBar, defStyle, 0);
    int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {
        int attr = a.getIndex(i);
        switch (attr) {
            case R.styleable.CustomProgressBar_firstColor:
                mFirstColor = a.getColor(attr, Color.GREEN);
                break;
            case R.styleable.CustomProgressBar_secondColor:
                mSecondColor = a.getColor(attr, Color.RED);
                break;
            case R.styleable.CustomProgressBar_circleWidth:
                mCircleWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                break;
            case R.styleable.CustomProgressBar_speed:
                mSpeed = a.getInt(attr, 20);// 默認 20
                break;
        }
    }
    a.recycle();
    mPaint = new Paint();
    // 繪圖線程
    new Thread() {
        public void run() {
            while (true) {
                mProgress++;
                if (mProgress == 360) {
                    mProgress = 0;
                    if (!isNext) isNext = true;
                    else isNext = false;
                }
                postInvalidate();
                try {
                    Thread.sleep(mSpeed);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
    }.start();
}

三、重寫 onDraw

由於是規則的圓形,這裡不需要重寫 onMeasure,直接重寫 onDraw 即可:

@Override
protected void onDraw(Canvas canvas) {
    int centre = getWidth() / 2; // 獲取圓心的 x 坐標
    int radius = centre - mCircleWidth / 2;// 半徑
    mPaint.setStrokeWidth(mCircleWidth); // 設置圓環的寬度
    mPaint.setAntiAlias(true); // 消除鋸齒
    mPaint.setStyle(Paint.Style.STROKE); // 設置空心
    RectF oval = new RectF(centre - radius, centre - radius,
            centre + radius, centre + radius); // 用於定義圓弧的形狀和大小的界限
    if (!isNext) {// 第一顏色的圈完整,第二顏色跑
        mPaint.setColor(mFirstColor);
        canvas.drawCircle(centre, centre, radius, mPaint); // 畫出圓環
        mPaint.setColor(mSecondColor);
        canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根據進度畫圓弧
    } else {
        mPaint.setColor(mSecondColor);
        canvas.drawCircle(centre, centre, radius, mPaint); // 畫出圓環
        mPaint.setColor(mFirstColor);
        canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根據進度畫圓弧
    }
}

至此大功告成。唯一比較需要斟酌的地方,就是兩個顏色何時切換、如何切換。我採用上述 isNext 旗標的方式,你也可以自行思考其他實作方式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved