1. 涉及到的技术点
- CountDownTimer的使用
- CardView卡片控件的使用
- 如何将程序启动的第一个页面设置为启动页或欢迎页
2. 实现步骤
- 创建倒计时布局文件
- 使用 CountDownTimer实现倒计时
- 如何将程序第一个页面设置为启动页或欢迎页
<?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="@mipmap/img_welcome"
tools:context=".WelcomeActivity">
<androidx.cardview.widget.CardView
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:backgroundTint="#20000000"
android:elevation="0dp"
app:cardCornerRadius="25dp">
<TextView
android:id="@+id/tv_countdown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="3 s"
android:textSize="16sp"
android:textStyle="bold" />
</androidx.cardview.widget.CardView>
</RelativeLayout>
public class WelcomeActivity extends AppCompatActivity {
private TextView tvCountdown;
private CountDownTimer countDownTimer;
private long timeLeftInMillis = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
tvCountdown = findViewById(R.id.tv_countdown);
startCountdown();
}
private void startCountdown() {
countDownTimer =new CountDownTimer(timeLeftInMillis,1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftInMillis = millisUntilFinished;
int secondsRemaining = (int) (millisUntilFinished / 1000);
tvCountdown.setText(secondsRemaining +" s");
}
@Override
public void onFinish() {
finish();
startActivity(new Intent(WelcomeActivity.this, ProductActivity.class));
}
}.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (countDownTimer != null) {
countDownTimer.cancel();
}
}
}
3. 运行效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/eb205951611d32524897d17eefdbe14d.gif#pic_center)
4. 视频教程
- 教程地址: https://www.bilibili.com/video/BV1zw411676Z/?vd_source=984bb03f768809c7d33f20179343d8c8