Android CountDownTimer实现定时器和倒计时效果
作者:lijiao 时间:2023-12-09 02:11:28
本文实例为大家分享了Android实现定时器和倒计时的具体代码,供大家参考,具体内容如下
直接上代码,相信都看得懂。
Android已经帮封装好了一个类,只不过很多人不知道而已。
代码:
public class SplashActivity extends BaseAppCompatActivity {
@InjectView(R.id.ivBg)
ImageView ivBg;
@InjectView(R.id.tvSkip)
TextView tvSkip;
int[] imgs = new int[]{
R.mipmap.irving,
R.mipmap.bryant,
R.mipmap.james,
R.mipmap.harden,
R.mipmap.curry};
private CountDownTimer timer;
@Override
protected int getContentViewLayoutID() {
return R.layout.activity_splash;
}
@Override
protected void initViewsAndEvents() {
int index = (int) (Math.random() * imgs.length);
ivBg.setImageResource(imgs[index]);
timer = new CountDownTimer(3500, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tvSkip.setText(String.format(getResources().getString(R.string.skip), (int) (millisUntilFinished / 1000 + 0.1)));
}
@Override
public void onFinish() {
tvSkip.setText(String.format(getResources().getString(R.string.skip), 0));
startActivity(new Intent(mContext, HomeActivity.class));
finish();
}
};
timer.start();
}
@OnClick(R.id.tvSkip)
public void skip() {
if (timer != null)
timer.cancel();
startActivity(new Intent(mContext, HomeActivity.class));
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
}
}
}
调用的时候很简单:timer.start();
最后说明一下:CountDownTimer timer = new CountDownTimer(3500, 1000)中,第一个参数表示总时间,第二个参数表示间隔时间。意思就是每隔一秒会回调一次方法onTick,然后10秒之后会回调onFinish方法。
布局activity_splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tvSkip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:background="@drawable/common_button_selector"
android:padding="5dp"
android:text="@string/skip" />
</RelativeLayout>
标签:Android,CountDownTimer,定时器,倒计时
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解三种C#实现数组反转方式
2023-10-09 20:29:23
基于java实现简单发红包功能
2023-08-09 17:06:40
![](https://img.aspxhome.com/file/2023/9/72559_0s.jpg)
C#通过委托调用Button单击事件的方法
2022-11-29 22:09:15
Android自定义View实现打字机效果
2022-01-13 11:03:41
![](https://img.aspxhome.com/file/2023/5/138995_0s.gif)
c# Selenium爬取数据时防止webdriver封爬虫的方法
2023-06-24 07:50:51
![](https://img.aspxhome.com/file/2023/7/59117_0s.png)
SpringBoot如何进行对象复制的实践
2023-11-23 03:40:19
![](https://img.aspxhome.com/file/2023/5/59935_0s.jpg)
浅析Android App的相对布局RelativeLayout
2023-04-08 18:28:48
![](https://img.aspxhome.com/file/2023/6/110676_0s.jpg)
Spring Cloud 网关服务 zuul 动态路由的实现方法
2022-04-09 20:17:46
![](https://img.aspxhome.com/file/2023/8/86868_0s.png)
聊聊java中引用数据类型有哪些
2022-01-10 11:59:10
Java 线程同步详解
2021-08-20 05:03:10
C#中Span相关的性能优化建议
2021-07-05 15:42:26
![](https://img.aspxhome.com/file/2023/7/69817_0s.png)
C#使用三层架构开发Winform的详细案例
2023-08-08 03:22:24
![](https://img.aspxhome.com/file/2023/9/67569_0s.jpg)
springboot中使用@Transactional注解事物不生效的坑
2021-10-03 10:01:47
![](https://img.aspxhome.com/file/2023/5/81905_0s.png)
详解Android Activity中的几种监听器和实现方式
2022-11-02 11:26:05
C# 启动 SQL Server 服务的实例
2022-02-10 21:59:38
解析Android游戏中获取电话状态进行游戏暂停或继续的解决方法
2023-09-09 00:31:09
详解Java Spring AOP
2023-09-06 15:40:02
![](https://img.aspxhome.com/file/2023/1/92641_0s.png)
Springboot整合Shiro的代码实例
2021-09-03 04:16:52
利用json2POJO with Lombok 插件自动生成java类的操作
2023-07-12 09:31:28
![](https://img.aspxhome.com/file/2023/8/69718_0s.jpg)
Android自定义View圆形进度条控件(三)
2021-11-13 10:17:51
![](https://img.aspxhome.com/file/2023/3/138183_0s.gif)