CountDownTimer 倒计时封装(可 暂停 继续 重新开始)
来源:互联网 发布:无线mac地址修改器 编辑:程序博客网 时间:2024/06/05 09:01
直接封转好了的 时间可以自己改DefauteMillisInFuture 这个值;
**以下为使用方法**//开始倒计时 CountDownTimerUtil.StartToCountDown(currentTime, this)//结束倒计时 currentTime = CountDownTimerUtil.StopCountDown();//完成倒计时@Overridepublic void onFinish() { currentTime = 0; CountDownTimerUtil.getDefault().setTimerListener(null); orderTimeTv.setText("倒计时完毕");}//每一步回调@Overridepublic void onNext(String[] leftTimeFormatedStrings) { orderTimeTv.setText( leftTimeFormatedStrings[1] + ":" + leftTimeFormatedStrings[2] + ":" + leftTimeFormatedStrings[3]);}**以下为倒计时源码(可以直接复制粘贴)**package com.banyuekj.drive.common;import android.os.CountDownTimer;import com.banyuekj.drive.ui.activity.DetailActivity;import com.example.commonlibrary.utils.DateUtil;/** * Created by MaQi on 2017/5/17. */public class CountDownTimerUtil { private static CountDownTimerUtil countDownTimerUtil; private static final long DefauteMillisInFuture = 60 * 1 * 1000; private static final long DefauteCountDownInterval = 1; private MyCountDownTimer countDownTimer; private CountDownTimerUtil(long millisInFuture, long countDownInterval) { countDownTimer = new MyCountDownTimer(millisInFuture, countDownInterval); } public static CountDownTimerUtil newInstance(long millisInFuture, long countDownInterval) { if (countDownTimerUtil == null) { countDownTimerUtil = new CountDownTimerUtil(millisInFuture, countDownInterval); } return countDownTimerUtil; } public MyCountDownTimer getCountDownTimer() { return countDownTimer; } public void setCountDownTimer(MyCountDownTimer countDownTimer) { this.countDownTimer = countDownTimer; } public static CountDownTimerUtil getDefault() { return newInstance(DefauteMillisInFuture, DefauteCountDownInterval); } public MyCountDownTimer setContinueCountDownTimer(long currentTime) { setCountDownTimer(new MyCountDownTimer(currentTime, DefauteCountDownInterval)); return getCountDownTimer(); } public MyCountDownTimer setContinueCountDownTimer() { setCountDownTimer(new MyCountDownTimer(DefauteMillisInFuture, DefauteCountDownInterval)); return getCountDownTimer(); } public void setTimerListener(DetailActivity activity) { setOnCountDownTimerListener(activity); } private OnCountDownTimerListener onCountDownTimerListener; public void setOnCountDownTimerListener(OnCountDownTimerListener onCountDownTimerListener) { this.onCountDownTimerListener = onCountDownTimerListener; } /** * 开始倒计时 */ public static void StartToCountDown(long currentTime, DetailActivity activity) { CountDownTimerUtil aDefault = CountDownTimerUtil.getDefault(); if (aDefault.getCountDownTimer().ismCancelled()) { if (currentTime > 0.005f) { //此时已经初始化过了 aDefault.setContinueCountDownTimer(currentTime).toStart(); } else if (currentTime == 0) { aDefault.setContinueCountDownTimer().toStart(); } else { aDefault.getCountDownTimer().toStart(); } aDefault.setTimerListener(activity); } } /** * 暂停倒计时 */ public static long StopCountDown() { CountDownTimerUtil aDefault = CountDownTimerUtil.getDefault(); aDefault.setTimerListener(null); return aDefault.getCountDownTimer().toStop(); } public class MyCountDownTimer extends CountDownTimer { private long currrntTime; private boolean mCancelled; public boolean ismCancelled() { return mCancelled; } public long getCurrrntTime() { return currrntTime; } public void setmCancelled(boolean cancelled) { if (mCancelled != cancelled) mCancelled = cancelled; } public void setCurrrntTime(long currrntTime) { this.currrntTime = currrntTime; } /** * @param millisInFuture 表示以毫秒为单位 倒计时的总数 * <p> * 例如 millisInFuture=1000 表示1秒 * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法 * <p> * 例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick() */ public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); mCancelled = true; } @Override public void onTick(long millisUntilFinished) { setCurrrntTime(millisUntilFinished); setmCancelled(false); onCountDownTimerListener.onNext(DateUtil.getLeftTimeFormatedStrings(millisUntilFinished)); } /** * 比预期的要晚上一秒钟··· */ @Override public void onFinish() { setmCancelled(true); onCountDownTimerListener.onFinish(); } public void toStart() { if (ismCancelled()) start(); } public long toStop() { if (!ismCancelled()) { setmCancelled(true); cancel(); } return getCurrrntTime(); } } public interface OnCountDownTimerListener { void onFinish(); void onNext(String[] leftTimeFormatedStrings); }}public static String[] getLeftTimeFormatedStrings(long leftTime) { String days = "00"; String hours = "00"; String minutes = "00"; String seconds = "00"; String millisSeconds = "000"; if (leftTime > 0) { //毫秒 long millisValue = leftTime % 1000; if (millisValue > 100) { millisSeconds = String.valueOf( millisValue); } else if (millisValue >= 10 && millisValue < 100) { millisSeconds = String.valueOf("0" + millisValue); } else { millisSeconds = String.valueOf("00" + millisValue); } //实际多少秒 long trueSeconds = leftTime / 1000; //当前的秒 long secondValue = trueSeconds % 60; if (secondValue < 10) { seconds = String.valueOf("0" + secondValue); } else { seconds = String.valueOf(secondValue); } //当前的分 long trueMinutes = trueSeconds / 60; long minuteValue = trueMinutes % 60; if (minuteValue < 10) { minutes = String.valueOf("0" + minuteValue); } else { minutes = String.valueOf(minuteValue); } //当前的小时数 long trueHours = trueMinutes / 60; long hourValue = trueHours % 24; if (hourValue < 10) { hours = String.valueOf("0" + hourValue); } else { hours = String.valueOf(hourValue); } //当前的天数 long dayValue = trueHours / 24; if (dayValue < 10) { days = String.valueOf("0" + dayValue); } else { days = String.valueOf(dayValue); } } return new String[]{days, hours, minutes, seconds, millisSeconds};}
总结:该方法中我尽量的使用单例;可能存在内存泄露 以及代码的冗余 有些问题还敬请大神指正。
阅读全文
0 0
- CountDownTimer 倒计时封装(可 暂停 继续 重新开始)
- 简单开始,暂停,继续的倒计时实现
- 关于时间倒计时 暂停 然后继续开始(延伸 li轮流显示 hover移入暂停 mouseover移出继续开始)
- c++设计一个计时器(能够暂停,重新开始,继续)的功能。写的不好,仅供参考!
- android开发——分秒计时器(可 开始/暂停/继续/停止)
- 短信发送倒计时按钮封装,退出再进入可继续倒计时,绝对时间倒计时
- Cocos2d-x游戏暂停、继续游戏、重新开始界面的实现---之游戏开发《赵云要格斗》(10)
- JS倒计时效果带暂停和继续
- Java/Android倒计时(开始,暂停,恢复,停止)
- android MediaRecorder录制视频(可暂停、继续)
- android倒计时功能(CountDownTimer)
- CountDownTimer倒计时
- CountDownTimer倒计时
- CountDownTimer倒计时
- CountDownTimer 倒计时
- CountDownTimer倒计时
- 倒计时CountDownTimer
- CountDownTimer,倒计时
- hihocoder1273(dfs)
- 【hadoop入门实战】Mac上安装hadoop,并运行程序(1)
- MyFlag Step5:app页面初步设计与代码编写
- B1021. 个位数统计 (15')
- Project from Version Control--Idea中使用git
- CountDownTimer 倒计时封装(可 暂停 继续 重新开始)
- [一起学Hive]之十-Hive中Join的原理和机制
- Spring 配置 hive
- 解耦与 MVC
- bond
- can not cast to android.support.v7.widget.ShareActionProvider
- JAVA中的Fork/Join框架
- 文章标题
- Java深入