App的开启页(Splash Screen)是用户打开应用时的第一个界面,一般用于展示应用的logo和品牌形象等,同时也可以进行一些必要的操作,比如加载数据等。在用户等待启动的时候,开启页可以起到缓冲的作用,让用户感觉应用更为顺畅。
制作开启页的方法主要有两种:
1. 使用ImageView展示图片
可以在layout中添加一个ImageView,设置成全屏并将启动页需要展示的logo或图片赋到该ImageView中。同时在代码中添加一个Handler或CountDownTime计时器,来实现启动页延迟消失并跳转到应用的首页。具体代码如下:
```
public class SplashActivity extends AppCompatActivity {
private ImageView mSplashView;
private int mTime = 5000;// 跳过倒计时提示5秒
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
mSplashView = findViewById(R.id.splash_view);
mSplashView.setImageResource(R.drawable.splash);// 加载图片资源
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
SplashActivity.this.finish();
}
}, mTime);
}
}
```
2. 使用Lottie动画展示启动页
与ImageView不同,Lottie动画可以实现更加丰富的展示效果,比如动态的加载过程、有趣的互动效果等。使用Lottie展示开启页的具体流程如下:
- 添加Lottie库
在项目的build.gradle文件中添加Lottie库的依赖项。
```
dependencies {
implementation 'com.airbnb.android:lottie:3.4.0'
}
```
- 添加动画文件
在assets文件夹中添加需要展示的Lottie动画文件,比如animation.json。
- 在layout中使用LottieView
在layout文件中添加一个LottieView,用于展示动画。同时,可以根据实际需要对动画进行设置,比如设置循环播放、设置背景色、设置动画速度等。
```
android:id="@+id/animation_view" android:layout_width="match_parent" android:layout_height="match_parent" app:lottie_fileName="animation.json" app:lottie_loop="false" app:lottie_autoPlay="true" app:lottie_backgroundColor="@color/colorPrimary" app:lottie_speed="1"/> ``` - 在代码中控制动画的播放 在SplashActivity中,根据动画播放的时间来控制页面的跳转。动态计算启动页的时间是需要注意的一点,需要确保开机时间少于SplashActivity展示的时间。同时,可以在页面跳转之前暂停动画的播放,以使启动页不会过早消失而导致应用的首页未能准备完毕。 ``` public class SplashActivity extends AppCompatActivity { private LottieAnimationView mAnimationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_splash); mAnimationView = findViewById(R.id.animation_view); mAnimationView.setAnimation("animation.json"); mAnimationView.loop(false); mAnimationView.playAnimation(); new Handler().postDelayed(new Runnable() { @Override public void run() { mAnimationView.pauseAnimation(); Intent intent = new Intent(SplashActivity.this, MainActivity.class); startActivity(intent); SplashActivity.this.finish(); } }, mAnimationView.getDuration()); } } ``` 以上就是制作App开启页的两种方法,开发者可以根据自己的需求,选择更为适合自己的实现方案。