編輯:關於 Android 程式設計
登入頁面裡那個「點擊發送驗證碼後開始倒數」的計時效果,幾乎每個 App 都用得到。與其每次都用老套路去堆 Handler 和線程,不如封裝一個專屬的 TextView 來搞定它。今天就用一個更清爽的方式來實現類似效果。
先看成品:

樣子樸素,但我們先回顧一下傳統的 Handler 與 TimerTask 是怎麼折騰的。
Handler 主要用來處理接收到的消息,這是它最核心的用法。感興趣的朋友可以翻翻 API,這裡就不展開了。第一步,定義一個 Handler 來處理 Message:
Handler handler = new Handler() {
public void handleMessage(Message msg) {
// 在這裡處理要做的事情
super.handleMessage(msg);
}
};
接著新建一個實現 Runnable 介面的線程類:
public class MyThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);// 線程暫停 1 秒,單位毫秒
Message message = new Message();
message.what = 1;
handler.sendMessage(message);// 發送消息
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
在需要啟動的地方加上這句即可:
new Thread(new MyThread()).start();
先定義定時器、定時任務與 Handler 句柄:
private final Timer timer = new Timer();
private TimerTask task;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 在這裡處理要做的事情
super.handleMessage(msg);
}
};
初始化計時器任務:
task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
啟動定時器:
timer.schedule(task, 2000, 2000);
兩種寫法都能實現倒數,但都顯得笨重。原因在于 Handler 往往要承載大量異步行為,能少往裡塞東西就少塞。於是我們干脆自定義一個 TextView 來專門解決這個問題。
包結構如下:

整個方案只有一個自定義 View 和一份 XML,想搬進自己的項目也很方便。
先在 XML 裡做初始化配置(因為它繼承自 TextView,你也可以完全用 Java 代碼來初始化):
然後獲取對象:
countingText1 = (countingTextView) findViewById(R.id.countingText1);
接著設定時間邏輯就大功告成了,是不是比開線程輕鬆多了?
countingText1.setInterpolator(new LinearInterpolator());
countingText1.animateFromZero(60, 60000);
源碼裡對關鍵方法都有註解,這裡再補充幾個常用設置:
//從 startValue 到 endValue 的動畫過程
animateText(Integer startValue, Integer endValue)
//從 0 到 endValue 的動畫過程
animateFromZero(Integer endValue)
//設置結束值
setEndValue(int endValue)
//設置開始值
setStartValue(int startValue)
//設置插值器
setInterpolator(TimeInterpolator interpolator)
最後貼一段核心原理供大家體會:
public void animateText(Integer startValue, Integer endValue) {
setStartValue(startValue);
setEndValue(endValue);
ValueAnimator animator = ValueAnimator.ofInt(startValue, endValue);
animator.setInterpolator(getInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
setText(String.format(getFormat(), animation.getAnimatedValue()));
}
});
animator.setEvaluator(new TypeEvaluator() {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
return Math.round(startValue + (endValue - startValue) * fraction);
}
});
animator.setDuration(getDuration());
animator.start();
}
核心思路很直白:默認設置好起止值,再用 ValueAnimator 持續刷新 UI 直到抵達最大值,時間區間由 getDuration() 方法掌控。
項目地址:https://github.com/ddwhan0123/BlogSample/tree/master/Countingtextview
下載地址:https://github.com/ddwhan0123/BlogSample/blob/master/Countingtextview/Countingtextview.zip
Android Otto 事件總線框架入門:用於解耦元件溝通
這次要介紹的是 Otto——Android 上被廣泛使用的事件總線(EventBus)框架,由 Square 出品。官網:https://square.github.io
GitHub 項目解析(十一):一個簡單卻強大的自定義廣告彈窗
上一篇講的是我最近寫的網二維碼掃描庫,核心掃描能力依賴 ZXing。做的過程裡發現集成步驟還是偏繁瑣,於是有了封裝成標準庫的想法——項目名 android-zxingLi
Android 開發筆記第七彈:動手做一個簡易秒表
本文是「簡易時鐘」系列的第三篇,前面兩篇分別完成了鬧鐘與計時器,這次我們來實作秒表,重點在於計時精度與分段記錄(Record)功能。 版面配置 同樣新建一個類別 Sto
用 RecyclerView 打造 Gallery 相冊:從基本用法到滾動聯動
RecyclerView 是 Android 5.0 Material Design 體系中的一員,同期的還有 CardView、Palette 等。顧名思義,它的核心賣