码迷,mamicode.com
首页 > 其他好文 > 详细

一个倒计时显示的毫秒数(模仿拼多多)

时间:2019-12-31 10:47:33      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:void   ext   down   null   long   stop   直接   help   样式   

直接上干货

/**
     * 毫秒倒计时成时 分 秒的形式
     * @param ms
     * @return
     */
    public static String[] formatSecKillTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;

        Long hour = (ms ) / hh;
        Long minute = (ms  - hour * hh) / mi;
        Long second = (ms  - hour * hh - minute * mi) / ss;
        Long milliSecond = (ms  - hour * hh - minute * mi - second * ss)/100;

        String[] strings = new String[4];
        String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
        String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
        String strSecond = second < 10 ? "0" + second : "" + second;//
        String strMilliSecond = "" + milliSecond;//毫秒
        strings[0]=strHour;
        strings[1]=strMinute;
        strings[2]=strSecond;
        strings[3]=strMilliSecond;
        return strings;
    }




//这里代表时分秒和分秒
String[] times = TimeHelper.formatSecKillTime(millisUntilFinished);
times[0] + ":" + times[1] + ":" + times[2] + ":" + times[3];

需要搭配倒计时的显示

public void countDownstart(long l) {
        if (countDownTimer == null) {
            countDownTimer = new CountDownTimer(l, 100) {
                public void onTick(long millisUntilFinished) {
                    String[] times = TimeHelper.formatSecKillTime(millisUntilFinished);
                    tv_countdown.setText(times[0] + ":" + times[1] + ":" + times[2] + ":" + times[3]);
                    LogUtil.d("CountDownTimer", times[0] + ":" + times[1] + ":" + times[2] + ":" + times[3]);
                }

                public void onFinish(String redpackage_id) {
                 
                    LogUtil.d("CountDownTimer", "完了啊");
                }
            };
        }
        //使用前记得先cancel
        countDownTimer.cancel();
        countDownTimer.setmStopTimeInFuture(l);
        countDownTimer.start();
    }

 

注意的一点之前的1000这里写为100导致倒计时快了10倍,所以就可以展示评多多样式

new CountDownTimer(l, 100)


磊磊

一个倒计时显示的毫秒数(模仿拼多多)

标签:void   ext   down   null   long   stop   直接   help   样式   

原文地址:https://www.cnblogs.com/widgetbox/p/12122844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!