CountDownTimer 倒计时封装(可 暂停 继续 重新开始)

直接封转好了的  时间可以自己改DefauteMillisInFuture 这个值;
**以下为使用方法**
//开始倒计时
 CountDownTimerUtil.StartToCountDown(currentTime, this)
 
//结束倒计时
 currentTime = CountDownTimerUtil.StopCountDown();

//完成倒计时
@Override
public void onFinish() {
	   currentTime = 0;
	   CountDownTimerUtil.getDefault().setTimerListener(null);
	   orderTimeTv.setText("倒计时完毕");
}
//每一步回调
@Override
public 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};
}

总结:该方法中我尽量的使用单例;可能存在内存泄露 以及代码的冗余 有些问题还敬请大神指正。

关注公众号领取更多干货
在这里插入图片描述

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值