Android Handler實(shí)現(xiàn)閃屏頁(yè)倒計(jì)時(shí)代碼
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
package com.zjx.todayinfomation;import android.os.Handler;public class CustomCountDownTimer implements Runnable{ // 1.實(shí)時(shí)去回調(diào) 這個(gè)時(shí)候是什么時(shí)間 倒計(jì)時(shí)到幾點(diǎn) 觀察者設(shè)計(jì)模式 // 2.支持傳入總時(shí)間 動(dòng)態(tài)傳入 // 3.每過(guò)一秒 總秒數(shù) -1 // 4.總時(shí)間倒計(jì)時(shí)為0時(shí)候 回調(diào)完成狀態(tài) private int time; // 總時(shí)間 private int countDowntime; // 倒計(jì)時(shí)事件 private IcountDownHandler countDownHandler; // 回調(diào)接口 private final Handler handler; // handler private boolean isRunning; // 是否允許 /** * @param time 傳入的總時(shí)間 * @param countDownHandler 接口回調(diào) */ public CustomCountDownTimer(int time,IcountDownHandler countDownHandler){ handler = new Handler(); this.time = time; this.countDowntime = time; // 倒計(jì)時(shí)時(shí)間第一次 就是總時(shí)間 this.countDownHandler = countDownHandler; } @Override public void run() { if (isRunning){ // 如果開(kāi)啟了 if (countDownHandler != null){ // 并且回調(diào)接口不為空 回調(diào)當(dāng)前秒數(shù) countDownHandler.onTicker(countDowntime); // countDowntime 第一次進(jìn)來(lái)就是總時(shí)間 比如5 } // 如果當(dāng)前秒數(shù)為0 回調(diào)完成 if (countDowntime == 0){ cancel(); if (countDownHandler != null){ countDownHandler.onFinish(); } }else{ // 如果當(dāng)前秒數(shù)部位0 每次 減少1秒 并且 疫苗后 重新執(zhí)行這個(gè)run方法 countDowntime = time--; // 5 4 3 2 1 類似 handler.postDelayed(this,1000); } } } /** * 開(kāi)啟run方法 */ public void start(){ isRunning = true; handler.post(this); // 調(diào)用run方法 } /** * 跳出循環(huán) 終止run方法 */ public void cancel(){ isRunning = false; handler.removeCallbacks(this); } /** * 觀察者 回調(diào)接口 */ public interface IcountDownHandler{ void onTicker(int time);// 回調(diào)事件 倒計(jì)時(shí) void onFinish(); // 回調(diào)完成 }}
補(bǔ)充知識(shí):android 列表每一個(gè)item都加上倒計(jì)時(shí)
使用recyclerview的問(wèn)題:當(dāng)前顯示的view如果滑到看不見(jiàn)再滑回來(lái)就會(huì)重新倒計(jì)時(shí)
解決方法是換用listview 使用viewholder復(fù)用原理 把倒計(jì)時(shí)寫(xiě)在第一次加載時(shí)候 這樣就不會(huì)出現(xiàn)上述問(wèn)題
以上這篇Android Handler實(shí)現(xiàn)閃屏頁(yè)倒計(jì)時(shí)代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解JSP 內(nèi)置對(duì)象request常見(jiàn)用法2. ASP.NET MVC實(shí)現(xiàn)下拉框多選3. ASP.NET MVC增加一條記錄同時(shí)添加N條集合屬性所對(duì)應(yīng)的個(gè)體4. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別5. 解決request.getParameter取值后的if判斷為NULL的問(wèn)題6. JSP中param動(dòng)作的實(shí)例詳解7. ASP.NET MVC實(shí)現(xiàn)本地化和全球化8. .Net反向代理組件Yarp用法詳解9. JS中的常見(jiàn)數(shù)組遍歷案例詳解(forEach, map, filter, sort, reduce, every)10. .NET中的MassTransit分布式應(yīng)用框架詳解
