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
原创粉丝点击