編輯:關於 Android 程式設計
自定義 View 系列走到最後一期,先來看成品。

相信讀到這裡,各位心中多少都已經有了自己的實作思路。以下把我的做法貼出來供參考。
先準備兩支畫筆——一支畫刻度線,一支畫中央數字:
/** 刻度畫筆 */
private Paint linePaint;
/** 文字畫筆 */
private Paint textPaint;
/** 進度 */
private int index = 0;
/** 已走過的刻度數 */
private int ruling = 0;
private void init() {
linePaint = new Paint();
linePaint.setColor(Color.BLACK);
linePaint.setStrokeWidth(5);
linePaint.setAntiAlias(true);
linePaint.setStyle(Style.FILL);
textPaint = new Paint(linePaint);
textPaint.setTextSize(70);
textPaint.setColor(Color.WHITE);
handler.sendEmptyMessage(0);
handler.sendEmptyMessageDelayed(1, 2000);
}
接著是最關鍵的 onDraw():
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 把原點搬到畫布中心,後續所有繪製都以中心為基準
canvas.translate(getWidth() / 2, getHeight() / 2);
// 填充背景
canvas.drawColor(getResources().getColor(R.color.gray));
// 刻度總數
int count = 30;
// 計算進度數字置中所需的位置
Rect rect = new Rect();
String indexsString = String.valueOf(index);
if (index < 10) {
indexsString = "0" + indexsString; // 補零,讓 01~09 對齊
}
textPaint.getTextBounds(indexsString, 0, indexsString.length(), rect);
canvas.drawText(indexsString, -rect.width() / 2, rect.height() / 2, textPaint);
// 旋轉畫布畫刻度,依 ruling 決定顏色
for (int i = 0; i < count; i++) {
if (i <= ruling) {
linePaint.setColor(getResources().getColor(R.color.pink));
} else {
linePaint.setColor(Color.BLACK);
}
canvas.drawLine(0, 100, 0, 120, linePaint);
canvas.rotate(360 / count, 0, 0);
}
}
這裡有兩個技巧值得拆解說明。
第一,canvas 的座標系是可以「搬」的。呼叫 translate(width/2, height/2) 之後,原點就落在檢視正中央,於是畫刻度只需要用「距離中心多遠」來思考,不必每條線都去做 + width/2 的加減。drawLine(0, 100, 0, 120) 的意思就是:在中心正下方 100 到 120 的位置畫一段線。
第二,canvas 的變換是「疊加」的。rotate(360 / count, 0, 0) 每次把座標系再轉一個角度,迴圈跑完就均勻繞了一圈。這種「畫一個單位、轉一次」的寫法,比用三角函數去算 30 組座標要簡潔得多,也不容易出錯。
要注意的是 Canvas 的變更並不會自動復原,若同一個 onDraw 後面還要畫別的東西,記得搭配 canvas.save() 與 canvas.restore(),否則座標系會被上一段繪製污染。
效果如下:

搞懂這個之後,第二個可說是手到擒來。
這次需要四支畫筆:底環、進度弧、內圈刻度、中央文字。
/** 圓環底 */
private Paint rectPaint;
/** 刻度 */
private Paint linePaint;
/** 填充 */
private Paint fillPaint;
/** 進度文字 */
private Paint textPaint;
private int index = 0;
private int index2 = 0;
public ProgressView2(Context context) {
this(context, null);
}
public ProgressView2(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ProgressView2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
rectPaint = new Paint();
rectPaint.setAntiAlias(true);
rectPaint.setStrokeWidth(20);
rectPaint.setStyle(Style.STROKE); // 只畫框線,不填實心
rectPaint.setColor(getResources().getColor(R.color.dark));
linePaint = new Paint(rectPaint);
linePaint.setStrokeWidth(20);
linePaint.setColor(getResources().getColor(R.color.pink));
fillPaint = new Paint(linePaint);
fillPaint.setStrokeWidth(1);
fillPaint.setColor(Color.BLACK);
fillPaint.setAlpha(60); // 半透明刻度
textPaint = new Paint();
textPaint.setStrokeWidth(5);
textPaint.setAntiAlias(true);
textPaint.setTextSize(70);
textPaint.setColor(Color.WHITE);
handler.sendEmptyMessage(0);
handler.sendEmptyMessage(1);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int count = 60; // 刻度數
canvas.drawColor(getResources().getColor(R.color.gray));
canvas.translate(getWidth() / 2, getHeight() / 2);
// 圓環範圍:以中心為原點的 200×200 方框
RectF rectF = new RectF(-100, -100, 100, 100);
String indexString = String.valueOf(index);
if (index < 10) {
indexString = "0" + indexString;
}
Rect rect = new Rect();
textPaint.getTextBounds(indexString, 0, indexString.length(), rect);
canvas.drawText(indexString, -rect.width() / 2, rect.height() / 2, textPaint);
// 底環
canvas.drawArc(rectF, 0, 360, false, rectPaint);
// 進度弧:從 12 點方向(-90 度)開始,掃過 index 度
canvas.drawArc(rectF, -90, index, false, linePaint);
// 內圈刻度
for (int i = 0; i < index2; i++) {
canvas.drawLine(0, -90, 0, -110, fillPaint);
canvas.rotate(360 / count, 0, 0);
}
}

這裡的關鍵是 drawArc() 的三個角度參數。startAngle 是起點,sweepAngle 是掃過的角度。Android 的 0 度落在 3 點鐘方向,順時針為正;因此要從 12 點鐘方向起算,就必須寫 -90(或等價的 270)。這是自定義圓形進度條最常出錯的地方。
另外,Paint.setStyle(Style.STROKE) 與 Style.FILL 的差異也要分清楚:STROKE 只描邊、FILL 填實心、FILL_AND_STROKE 兩者都做。圓環要用 STROKE,靠 setStrokeWidth(20) 決定粗細。
三期自定義 View 看下來,其實核心就那麼幾件事:
在 onMeasure 中決定尺寸——想支援 wrap_content 就必須自己算預設值,否則檢視會被量成 0。
在 onDraw 中決定外觀——善用 translate、rotate、save/restore,能讓座標計算大幅簡化。
用 Paint 決定質感——抗鋸齒、粗細、樣式、透明度、漸層,畫面精緻度幾乎都在這裡。
用自定義屬性對外開放設定——透過 attrs.xml 搭配 obtainStyledAttributes(),讓使用者能在 XML 裡調整顏色與範圍,這才是「可重用的自定義控件」與「寫死的美工稿」的分野。
剩下的,就是多畫、多調、多看真機效果了。
Android Studio 實用指南:文本搜索工具
Android Studio 提供強大的文本搜索功能,可在編輯器或日志窗口中使用。Windows/Linux 可使用 Ctrl+F,macOS 可使用 Command+F
Android Studio 導入項目的幾種常見方法
版本與建議本文以 Android Studio 1.0 與 Eclipse ADT 為背景,整理常見的專案導入方式。Android Studio 預設使用 Gradle
Android LayoutInflater 深入解析:inflate 三個參數的差異
常見誤解 許多開發者會以為 inflate(resource, null)、inflate(resource, root, false) 與 inflate(resour
Android 快速开发环境搭建(Eclipse 版,附完整资源)
对很多新手来说,自己从零搭建一套开发环境并不轻松;即便是有经验的开发者,折腾环境也常常是件耗时费力的事。不过只要把相关资源都整理到位,快的话半小时到一小时就能搞定。我这篇