Android引导页倒计时CountDownTimer

ConutDownTimer

需求描述:

引导页右上角进行倒计时,结束后跳转

演示:

在这里插入图片描述

代码演示

activity_launcher.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/kun"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_countDownTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        android:text="跳过"
        android:textColor="@color/black"
        android:textSize="20sp" />

</RelativeLayout>

LauncherActivity

public class LauncherActivity extends AppCompatActivity {

    private ActivityLauncherBinding binding;
    private CountDownTimer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLauncherBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initCountDownTimer();
    }

    private void initCountDownTimer() {
        /**
         * 参数 - millisInFuture:设置倒计时的总时间(毫秒)
         * 参数 - countDownInterval:设置每次减去的时间(毫秒)
         */
        timer = new CountDownTimer(1000 * 5, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // 判断当前Activity是否isFinishing(),
                // 避免在finish,所有对象都为null的状态下执行CountDown造成内存泄漏
                if (!isFinishing()) {
                    int time = (int) millisUntilFinished;
                    binding.tvCountDownTimer.setText(time / 1000 + "跳过");
                    binding.tvCountDownTimer.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            checkToJump();
                        }
                    });
                }
            }

            @Override
            public void onFinish() {
                checkToJump();
            }
        }.start();
    }

    private void checkToJump() {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));

        //回收内存
        destroyTimer();
        finish();
    }

    private void destroyTimer() {
        // 避免内存泄露
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}

ConutDownTimer方法描述

1、CountDownTimer 直接 new 出来使用,其构造函数

    public CountDownTimer(long millisInFuture, long countDownInterval) {
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    }

参数 - millisInFuture:设置倒计时的总时间(毫秒)

参数 - countDownInterval:设置每次减去的时间(毫秒)

2、方法

public final void cancel ()

public abstract void onFinish ()

public abstract void onTick (long millisUntilFinished)

public final CountDownTimer start ()

  • cancel() 取消当前任务
  • onFinish() 当前任务完成的时候调用
  • onTick(long millisUntilFinished) 当前任务每完成一次倒计时间隔时间时回调
  • start() 开始当前的任务
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值