1. 涉及到的技术点
- 使用CountDownTimer实现倒计时
- 使用ViewCompat.animate实现logo缩放动画
2. 代码实现过程
- activity_welcome.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"
tools:context=".WelcomeActivity">
<androidx.cardview.widget.CardView
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:backgroundTint="@color/purple_200"
app:cardCornerRadius="20dp"
app:cardElevation="0dp">
<TextView
android:id="@+id/tv_countdown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="3 | 跳转"
android:textColor="@color/white"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleX="1.6"
android:scaleY="1.6"
app:cardCornerRadius="25dp"
app:cardElevation="0dp">
<ImageView
android:layout_width="110dp"
android:layout_height="110dp"
android:src="@mipmap/img_logo" />
</androidx.cardview.widget.CardView>
</RelativeLayout>
- WelcomeActivity.java页面
public class WelcomeActivity extends AppCompatActivity {
private TextView tvCountdown;
private CardView card_logo;
private CountDownTimer countDownTimer;
private long timeLeftInMillis = 4000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
tvCountdown = findViewById(R.id.tv_countdown);
card_logo = findViewById(R.id.card_logo);
startCountdown();
startAnim();
}
private void startAnim() {
ViewCompat.animate(card_logo)
.scaleX(1.0f)
.scaleY(1.0f)
.setDuration(1000)
.setListener(new ViewPropertyAnimatorListener() {
@Override
public void onAnimationStart(View view) {
}
@Override
public void onAnimationEnd(View view) {
}
@Override
public void onAnimationCancel(View view) {
}
})
.start();
}
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() {
startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
finish();
}
}.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (countDownTimer != null) {
countDownTimer.cancel();
}
}
}
3. 运行效果