編輯:關於 Android 程式設計
自定義 View 練了一陣子之後,大致能寫出一些實用的小控件了。某天在家用手機,正好沒電在充電,看到那個充電動畫覺得挺有意思,就拆解了一下裡面的元素,心想這個我也能畫出來——於是有了這篇紀錄。

靜態圖看不出太多東西,但加上動畫之後還頗有質感。寫久了會發現,自定義 View 很像在 Photoshop 裡畫圖:建立圖層,然後在圖層上畫出想要的形狀。這裡也是同樣思路——畫面拆開來其實只有四段弧線、兩個圓,還有一圈時鐘刻度。想清楚這幾塊,實作難度就低了,原理與先前寫過的「弧線進度控件」大體相通。
新建 VivoView 並繼承 View,重載三個構造函數(其中兩個直接呼叫 initView()):
// 五支畫筆:外弧、粗弧、內圓、刻度、文字 private Paint mSmileRing, mBigRing, mInCrilePaint, mInLine, mTextPaint; // 控件寬高 private float mWidth, mHeight; // 圓角矩形範圍 private RectF mRectF; // 四段弧線的起始角度與掃描角度 private float startAngle = 270, startAngle2 = 270, startAngle3 = 270, startAngle4 = 270, sweepAngle = 90; // 中央文字與字級 private String text = "70%"; private float tvSize = 80; // 刻度進度 private float progress;
private void initView() {
mSmileRing = new Paint();
mSmileRing.setAntiAlias(true);
mSmileRing.setStrokeWidth(5);
mSmileRing.setStyle(Style.STROKE);
mSmileRing.setColor(Color.parseColor("#12ADFF"));
mBigRing = new Paint();
mBigRing.setAntiAlias(true);
mBigRing.setStrokeWidth(20);
mBigRing.setStyle(Style.STROKE);
mBigRing.setColor(Color.parseColor("#12ADFF"));
mInCrilePaint = new Paint();
mInCrilePaint.setAntiAlias(true);
mInCrilePaint.setStrokeWidth((float) 0.5);
mInCrilePaint.setStyle(Style.STROKE);
mInCrilePaint.setColor(Color.parseColor("#eeeeee"));
mInLine = new Paint();
mInLine.setAntiAlias(true);
mInLine.setStrokeWidth(3);
mInLine.setColor(Color.parseColor("#00ff00"));
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setStrokeWidth(3);
mTextPaint.setTextSize(tvSize);
mTextPaint.setColor(Color.parseColor("#ffffff"));
}
這段主要是設定筆寬、樣式與顏色,別忘了最後要在構造函數中呼叫它。
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvasOutArc1(canvas, mRectF);
canvasOutArc2(canvas, mRectF);
canvasOutArc3(canvas, mRectF);
canvasOutArc4(canvas, mRectF);
drawCircle(canvas);
drawCircleIn(canvas);
canvasDrawText(canvas);
}
把每一個元素抽成獨立方法,可讀性與維護性都好很多。四段弧線的做法一致,只是 RectF 內縮比例、筆寬與掃描角度不同:
// 最外層細弧
private void canvasOutArc1(Canvas canvas, RectF mRectF) {
mRectF = new RectF((float) (mWidth * 0.1), (float) (mWidth * 0.1),
(float) (mWidth * 0.9), (float) (mWidth * 0.9));
canvas.drawArc(mRectF, startAngle, sweepAngle + 90, false, mSmileRing);
}
// 外層粗弧
private void canvasOutArc2(Canvas canvas, RectF mRectF) {
mRectF = new RectF((float) (mWidth * 0.14), (float) (mWidth * 0.14),
(float) (mWidth * 0.85), (float) (mWidth * 0.85));
canvas.drawArc(mRectF, startAngle2, sweepAngle + 30, false, mBigRing);
}
// 內層細弧
private void canvasOutArc3(Canvas canvas, RectF mRectF) {
mRectF = new RectF((float) (mWidth * 0.22), (float) (mWidth * 0.22),
(float) (mWidth * 0.795), (float) (mWidth * 0.795));
canvas.drawArc(mRectF, startAngle3, sweepAngle, false, mSmileRing);
}
// 內層粗弧
private void canvasOutArc4(Canvas canvas, RectF mRectF) {
mRectF = new RectF((float) (mWidth * 0.255), (float) (mWidth * 0.255),
(float) (mWidth * 0.75), (float) (mWidth * 0.75));
canvas.drawArc(mRectF, startAngle4, sweepAngle, false, mBigRing);
}
接著是兩個內切圓。外圈那個還包含了 72 根刻度線——用 canvas.rotate() 每次轉 5 度畫一格,剛好繞一圈;已達到的進度綠色,未到達的灰色:
private void drawCircle(Canvas canvas) {
float radius = (float) (mHeight - (mHeight * 0.3) * 2 - (mWidth * 0.17));
float yuanX = (float) (mHeight / 2);
float yuanY = (float) (mWidth / 2);
canvas.drawCircle(yuanX, yuanY, radius, mInCrilePaint);
canvas.save();
float nowWidth = getMeasuredWidth();
float nowHeight = getMeasuredHeight();
for (int i = 0; i < 72; i++) {
mInLine.setColor(i >= progress ? Color.parseColor("#555555") : Color.parseColor("#00ff00"));
canvas.drawLine(nowWidth / 2, (float) (nowHeight / 2 - nowWidth / 2 + mWidth / 3.7),
nowWidth / 2, (float) (nowHeight / 2 - nowWidth / 2 + mWidth * 0.05 + mWidth / 3.7),
mInLine);
canvas.rotate(5, getWidth() / 2, getHeight() / 2);
}
}
private void drawCircleIn(Canvas canvas) {
float radius = (float) (mHeight - (mHeight * 0.3) * 2 - (mWidth * 0.22));
canvas.drawCircle((float) (mHeight / 2), (float) (mWidth / 2), radius, mInCrilePaint);
canvas.save();
}
private void canvasDrawText(Canvas canvas) {
float textSize = mTextPaint.measureText(text);
float x = mWidth / 2 - textSize / 2;
float y = mHeight / 2 + textSize / 5;
canvas.drawText(text, x, y, mTextPaint);
}
最後對外提供一個驅動動畫的方法:四段弧線的起始角互相呼應(一組順時針、一組逆時針),刻度進度則由外部傳入,並用 postInvalidateDelayed() 週期性重繪:
public void setData(int startAngle, float d) {
this.startAngle = startAngle;
this.startAngle2 = 360 - startAngle;
this.startAngle3 = startAngle;
this.startAngle4 = 360 - startAngle;
progress = d / 4;
postInvalidateDelayed(500);
}
布局中直接引用完整包名的自定義 View:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
tools:context=".MainActivity">
<com.example.vivoopen.weight.VivoView
android:id="@+id/vivo"
android:layout_width="180dip"
android:layout_height="180dip"
android:layout_centerInParent="true" />
</RelativeLayout>
Activity 用一個子線程逐步加大角度,透過 Handler 把數值送回主線程刷新:
public class MainActivity extends Activity {
private VivoView view;
private boolean isRun = true;
private int n2 = 2;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
int a = (Integer) msg.obj;
view.setData(a, a);
break;
case 2:
n2 = 359;
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (VivoView) findViewById(R.id.vivo);
new Thread(new Runnable() {
public void run() {
synchronized (view) {
while (isRun) {
for (int i = 0; i < n2; i = i + 10) {
Message msg = new Message();
msg.obj = i;
msg.what = 1;
SystemClock.sleep(100);
handler.sendMessage(msg);
}
handler.sendEmptyMessage(2);
}
}
}
}).start();
}
}
實作上有一點值得注意:drawCircle() 裡呼叫了 canvas.save() 卻沒有 restore(),每次 onDraw 都會讓 Canvas 的堆疊一層層累積,建議補上 canvas.restore() 配對,或是乾脆改用 saveLayer/restore 的標準寫法。另外,用 postInvalidateDelayed() 自驅動動畫在頁面不可見時仍會持續執行,記得在 onDetachedFromWindow() 中停止,才是好的自定義 View 習慣。
Android 仿騰訊 QQ 實現滑動刪除(附完整源碼)
看了許多高手寫的文章,受益良多,也非常欣賞大家的分享精神,於是決定開始寫 Blog,把自己的心得記錄下來與各位交流。 先來看看效果圖: 原本我打算在 ListView
Android 逐幀動畫播放的實作步驟
接下來會陸續整理一些 Android 開發中常用的實用技巧,這一篇先從最基礎的動畫播放談起。 做法總覽 Android 的逐幀動畫本質上就是「按順序快速切換多張圖片」,
Android 動態載入版面配置:不用 TabHost 也能換頁
前陣子專案有個需求:同一個頁面要依不同按鈕載入不同的版面。原本想用 TabHost,但美工給的設計稿完全用不上頁籤樣式,所以改走「動態載入」這條路。整理成 DEMO 記錄
Android Toast 提示元件入門:使用方式、自訂版面與顯示效果
在 Android 中,Toast 是一種輕量級提示元件。它不會搶走使用者焦點,也不會打斷目前操作,常用來顯示短暫的成功、失敗或提醒訊息。Toast 的基本用法最常見的做