Android 启动屏不正确的实现可能会导致用户长时间等待, 或者可能会出现黑白屏这里简单演示如何正确实现 Android 启动屏
演示分为以下几个步骤:
在 res/drawable 文件夹中创建 splash_background.xml 文件
编辑 res/values/styles.xml
创建 java/.../SplashActivity
编辑 manifests/AndroidManifest.xml
1 在 res/drawable 文件夹中创建 splash_background.xml 文件
根据你的需求调整位图图像的重力和尺寸
- <?xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@color/colorPrimary"/>
- <item android:gravity="center" android:width="100dp" android:height="100dp">
- <bitmap
- android:gravity="fill_horizontal|fill_vertical"
- android:src="@drawable/logo"/>
- </item>
- </layer-list>
2 编辑 res/values/styles.xml
这里的样式用于启动画面 这是为了在启动屏幕时隐藏操作栏
- <resources>
- <!-- Base application theme. -->
- <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
- <!-- Customize your theme here. -->
- <item name="colorPrimary">@color/colorPrimary</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="colorAccent">@color/colorAccent</item>
- </style>
- <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
- <item name="android:windowBackground">@drawable/splash_background</item>
- </style>
- </resources>
3 创建 java/.../SplashActivity
一旦 App 启动, SplashActivity 将启动, 然后转移到 MainActivity
- package com.example.jtdan.goodSplash;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- public class SplashActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //switch from splash activity to main activity
- Intent intent = new Intent(this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- }
4 编辑 manifests/AndroidManifest.xml
在清单文件中添加新的启动画面 Activity
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.jtdan.goodSplash">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="goodSplash"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name="com.example.jtdan.goodSplash.SplashActivity" android:theme="@style/SplashTheme">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name="com.example.jtdan.goodSplash.MainActivity"></activity>
- </application>
- </manifest>
示例源码地址: https://github.com/mrjoedang/goodSplash
来源: https://majing.io/posts/10000006631219