Android倒計時專案中的應用
阿新 • • 發佈:2018-12-04
今天拿到UI圖,看到很多產品模組中涉及到倒計時,本想偷偷懶在網上copy一份原始碼,在覽閱中發現並不適合多個模組管理,最後決定親自擼起。
UI圖
原始碼中主要分為兩個類
- CountdownManager類
* 倒計時管理器,所有需要倒計時的頁面,都需要新增監聽介面,避免每個模組單獨起執行緒,浪費時間。 在退出模組時 必須呼叫unRegisterCountDownTimer方法,否則會造成記憶體洩露。*
private ArrayList<CountDownTimer> timers = new ArrayList<>();
private static CountdownManager ourInstance = new CountdownManager();
public static CountdownManager getInstance() {
return ourInstance;
}
private CountdownManager() {
}
public void registerCountDownTimer(CountDownTimer countDownTimer) {
if (countDownTimer.remainTime > 0 ) {
timers.add(countDownTimer);
}
if (timers.size() == 1) {
new Thread(this).start();
}
}
public void unRegisterCountDownTimer(CountDownTimer countDownTimer) {
if (countDownTimer.remainTime>1)
countDownTimer.remainTime=1 ;
}
@Override
public void run() {
while (timers.size() > 0) {
handler.sendEmptyMessage(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
ArrayList<CountDownTimer> completeTimes = new ArrayList<>();
Handler handler;
{
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
synchronized (this) {
for (CountDownTimer timer : timers) {
timer.remainTime--;
int second = (int) (timer.remainTime % 60);
int minute = (int) (timer.remainTime / 60 % 60);
int hour = (int) (timer.remainTime / 60 / 60);
timer.onTimeChange(hour, minute, second, timer.remainTime);
if (timer.remainTime <= 0) {
completeTimes.add(timer);
}
}
timers.removeAll(completeTimes);
completeTimes.clear();
}
}
};
}
主要操作和管理都放在這個類中
- CountDownTimer介面
作用:配合管理類時間變化進行回撥
public long remainTime;
public TextView tv_hour,tv_minute,tv_second,textView;
public abstract void onTimeChange(int hour, int minute, int second, long remainTime);
}
remainTime:設定的倒計時時間,第二列變數分別表示:時,分,秒,擴充套件的(view),第二列並不是必須使用,因專案而定
- 如何快速遷移到專案中:
- 複製以上兩個類到專案中
- 註冊監聽,設定倒計時時間(秒)
@OnClick(R.id.btn)
public void onClicktime() {
timer = new Timer();
timer.remainTime = 3700;
time_tv.setEnabled(false);
CountdownManager.getInstance().registerCountDownTimer(timer);
Log.v("registerCountDownTimer ", timer + "註冊時間");
}
- 實現CountDownTimer介面,回撥中處理時間倒計時顯示
class Timer extends CountDownTimer {
@Override
public void onTimeChange(int hour, int minute, int second, long remainTime) {
if (remainTime <= 0) {
time_tv.setText("重新發送驗證碼");
time_tv.setEnabled(true);
if (timer != null) {
CountdownManager.getInstance().unRegisterCountDownTimer(timer);
timer = null;
}
Log.v("registerCountDownTimer ", hour + "===時1" + minute + "===分" + second + "====秒" + timer);
} else {
Log.v("registerCountDownTimer ", hour + "===時" + minute + "===分" + second + "====秒" + timer);
time_tv.setText("倒計時(h:m:s)格式:" + hour + ":" + minute + ":" + second + " ,S(格式):" + remainTime);
}
}
}
- 重要一點:退出進行銷燬
@Override
public void finish() {
if (timer != null) {
Log.v("registerCountDownTimer", timer + "銷燬時間");
CountdownManager.getInstance().unRegisterCountDownTimer(timer);
timer = null;
}
super.finish();
}
想get更多優質原始碼,關注專屬開發者公眾號:開發者原始碼。
所有的原始碼都在這裡~
原始碼下載地址:http://download.csdn.net/detail/qq_15895655/9780708