本篇实现效果图:
新建分别一个空的Activity,命名为SplashActivity
和GuideActivity
(GuideActivity是app 引导页,效果下一步实现~):
1. SplashActivity.class
package com.example.myapp.activity;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import com.example.myapp.R;
public class SplashActivity extends AppCompatActivity {
private LinearLayout ll;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ll = findViewById(R.id.main_ll);
//设置渐变效果
setAlphaAnimation();
}
/**
* 设置渐变效果
*/
private void setAlphaAnimation() {
//生成动画对象
AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f);
//设置持续时间3s
animation.setDuration(3000);
//给控件设置动画
ll.setAnimation(animation);
//设置动画监听
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
jump2Activity();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
/**
* 根据首次启动应用与否跳转到相应界面
*/
private void jump2Activity() {
SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
String First = sharedPreferences.getString("isFirst", "0");
Intent intent = new Intent();
if ("0".equals(First)) {
intent.setClass(this, GuideActivity.class);
}else{
intent.setClass(this, MainActivity.class);
}
startActivity(intent);
finish();
}
}
2. activity_splash
<?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"
android:orientation="vertical">
<LinearLayout
android:id="@+id/main_ll"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/main_iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/main_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:text="我是splsh页" />
</LinearLayout>
</RelativeLayout>