• 拓展 Android 原生 CountDownTimer 倒计时


    拓展 Android 原生 CountDownTimer 倒计时

    CountDownTimer

    在系统的CountDownTimer上进行的修改,主要是拓展了功能,当然也保留了系统默认的模式.

    四种模式:

    • Normal模式: 向上取整(我觉得应该是日常中用的最多的)
    • Floor模式: 向下取整
    • System模式: 系统默认的(保留系统原始功能)
    • SystemFix模式: 系统默认会少一个onTick()回调,这里只是把缺的这个回调加进去

    在Activity中的代码如下:

    final CountDownTimer timer=new CountDownTimer(10000,1000,CountDownTimer.NORMAL) {
                @Override
                public void onTick(long millisUntilFinished) {
                    Log.d("test","millisUntilFinished: "+millisUntilFinished);
                    long l = millisUntilFinished / 1000;
                    mTs.setText(""+l);
                }
    
                @Override
                public void onFinish() {
                    Log.d("test","onFinish 0");
                    mTs.setText("0");
                }
            };
    
            mBt_start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    timer.start();
                }
            });
    

    对应模式的Log如下所示:

    Normal 模式

    08-10 09:23:53.595 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 10000
    08-10 09:23:54.595 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 9000
    08-10 09:23:55.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 8000
    08-10 09:23:56.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 7000
    08-10 09:23:57.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 6000
    08-10 09:23:58.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 5000
    08-10 09:23:59.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 4000
    08-10 09:24:00.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 3000
    08-10 09:24:01.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 2000
    08-10 09:24:02.615 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 1000
    08-10 09:24:03.605 27628-27628/didikee.com.demoapk D/test: onFinish 0
    

    Floor 模式

    08-10 09:26:54.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 9000
    08-10 09:26:55.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 8000
    08-10 09:26:56.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 7000
    08-10 09:26:57.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 6000
    08-10 09:26:58.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 5000
    08-10 09:26:59.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 4000
    08-10 09:27:00.465 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 3000
    08-10 09:27:01.465 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 2000
    08-10 09:27:02.465 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 1000
    08-10 09:27:03.465 27628-27628/didikee.com.demoapk D/test: onFinish 0
    

    System 模式

    08-10 09:29:03.035 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 9999
    08-10 09:29:04.035 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 8998
    08-10 09:29:05.035 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 7996
    08-10 09:29:06.035 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 6995
    08-10 09:29:07.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 5993
    08-10 09:29:08.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 4992
    08-10 09:29:09.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 3990
    08-10 09:29:10.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 2989
    08-10 09:29:11.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 1987
    08-10 09:29:13.035 27628-27628/didikee.com.demoapk D/test: onFinish 0
    

    SystemFix 模式

    08-10 09:29:59.795 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 9999
    08-10 09:30:00.795 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 8998
    08-10 09:30:01.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 7997
    08-10 09:30:02.795 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 6997
    08-10 09:30:03.795 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 5996
    08-10 09:30:04.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 4994
    08-10 09:30:05.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 3992
    08-10 09:30:06.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 2990
    08-10 09:30:07.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 1989
    08-10 09:30:08.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 988
    08-10 09:30:09.795 27628-27628/didikee.com.demoapk D/test: onFinish 0
    

    总结

    项目里需要倒计时,用到的时候遇到了这个问题,查的时候别人也发现了,但是没看到有人修改,所以自己做了些改动,需要的小伙伴可以直接拿走.=.=
    代码在这里: https://github.com/didikee/Android-Learning-Report

    附言

    有的小伙伴肯定嫌麻烦,谁会点开那么慢的Github,更不会注册账户Star或者Fork的,如果遇到你可以这样解决"丢一秒"的问题.

    private CountDownTimer robLiftTimer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
    
                Message msg = Message.obtain();
                msg.what = 2;
                float v = millisUntilFinished * 1.0f / 1000f;
                int round = Math.round(v);
                msg.arg1 = round;
                Log.e("live", millisUntilFinished + "--" + round + "==" + v);
                mHandler.sendMessage(msg);
                if (round == 2) {//第二秒的时候"补"一秒
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Message msg = Message.obtain();
                            msg.what = 2;
                            msg.arg1 = 1;
                            mHandler.sendMessage(msg);
                        }
                    }, 1000);
                }
            }
    

    也很简单,对吧.=.=

  • 相关阅读:
    centos7 计划任务 定时运行sh
    Nginx负载均衡配置 域名和IP 访问时从java request.getServerName()获取不同问题解决
    windows2012激活方式 2016激活方式 windows10激活方式
    CentOS7 安装Chrome
    使用 go-cqhttp 在龙芯和其他平台搭建qq机器人
    KDE桌面无故特效消失和图标黑底
    SDUST 小学期飞机大战简述
    SDUST 小学期飞机大战简述
    宝塔中极速安装的PHP如何使用AMQP连接RabbitMQ
    Electron登录注册桌面应用源码+安装文件的打包方法
  • 原文地址:https://www.cnblogs.com/didikee/p/5755581.html
Copyright © 2020-2023  润新知