編輯:關於 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();
}
由於是規則的圓形,這裡不需要重寫 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 旗標的方式,你也可以自行思考其他實作方式。
Android 啟動白屏或黑屏閃現問題排查與優化
App 啟動時短暫閃現白屏或黑屏,是許多開發者都會遇到的問題。它通常不是 Crash,而是主題背景與啟動時序共同造成的視覺現象。 白屏與黑屏的原因 若 App 主題使用
自定義下拉刷新與上拉載入 ListView
自定義刷新列表的核心思路 下拉刷新與上拉載入可以透過自定義 ListView 完成。核心是在原本的 ListView 上加入自訂的 header 與 footer,並依手
Android 筆記(三):專案檔案結構逐項說明
第一次打開 Android 專案時,那一堆資料夾實在不太友善。這篇筆記把每個目錄與關鍵檔案的用途整理清楚,後續開發會順手很多。 1. src 資料夾 存放所有 Ja
Android 事件注入的三種實現路徑對比
在 Android 上做自動化操作、腳本回放或者測試框架,經常繞不開一個問題:怎麼把按鍵和觸摸事件「塞」進系統裡。實踐中大致有三條路可走,它們的可行性、副作用和適用邊界各