SplashScreen例子-代码详细版本
以下是一个完整详细的 Android 启动页(SplashScreen)示例项目,包括:
• 项目结构
• 所有必要的 XML 资源(主题、布局、图标)
• MainActivity 与 ViewModel 的完整代码
• 整体启动流程说明
基于 Android 12+ 的官方 SplashScreen API,并兼容旧版本
项目结构
app/
├── java/
│ └── com.example.splashdemo/
│ ├── MainActivity.kt
│ └── SplashViewModel.kt
├── res/
│ ├── layout/
│ │ └── activity_main.xml
│ ├── drawable/
│ │ ├── ic_splash_logo.xml <-- AnimatedVectorDrawable
│ ├── animator/
│ │ └── fade_in.xml <-- 动画资源
│ ├── values/
│ │ ├── colors.xml
│ │ ├── themes.xml
│ │ └── strings.xml
├── AndroidManifest.xml
1️⃣ 添加依赖
dependencies {
implementation "androidx.core:core-splashscreen:1.0.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.2"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
}
2️⃣ AndroidManifest.xml
<application
android:theme="@style/Theme.MyApp.Splash">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.MyApp.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
3️⃣ themes.xml
<!-- res/values/themes.xml -->
<resources>
<!-- 启动页主题 -->
<style name="Theme.MyApp.Splash" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowSplashScreenBackground">@color/white</item>
<item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_splash_logo</item>
<item name="android:windowSplashScreenAnimationDuration">400</item>
<item name="android:postSplashScreenTheme">@style/Theme.MyApp</item>
</style>
<!-- 应用主主题 -->
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
</style>
</resources>
4️⃣ activity_main.xml
<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:id="@+id/tv_message"
android:layout_gravity="center"
android:text="加载中..."
android:textSize="24sp"
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
5️⃣ ic_splash_logo.xml(Vector 动画图标)
<!-- res/drawable/ic_splash_logo.xml -->
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_launcher_foreground">
<target android:name="vector" android:animation="@animator/fade_in" />
</animated-vector>
6️⃣ fade_in.xml
<!-- res/animator/fade_in.xml -->
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="alpha"
android:duration="400"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType" />
7️⃣ MainActivity.kt
package com.example.splashdemo
import android.os.Bundle
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.viewModels
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
class MainActivity : ComponentActivity() {
private val viewModel: SplashViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
// 启用 SplashScreen API(必须在 super.onCreate 之前)
val splashScreen = installSplashScreen()
// 设置 splash 保留条件:加载未完成前不移除 splash
splashScreen.setKeepOnScreenCondition {
viewModel.isLoading
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tvMessage = findViewById<TextView>(R.id.tv_message)
viewModel.loadData {
tvMessage.text = it
}
}
}
8️⃣ SplashViewModel.kt
package com.example.splashdemo
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class SplashViewModel : ViewModel() {
var isLoading: Boolean = true
private set
fun loadData(onDataReady: (String) -> Unit) {
viewModelScope.launch {
delay(2000) // 模拟加载过程
isLoading = false
onDataReady("欢迎回来!数据加载完成")
}
}
}
启动效果说明
1. 启动时自动加载 splash 图标 + 背景;
2. MainActivity 初始化时阻止 splash 移除,直到 loadData() 完成;
3. 数据加载完后,Splash 自动移除,界面展示主内容;
4. 无需创建多余的 SplashActivity,结构简洁清晰。
支持的 Android 版本
• Android 12+:系统原生控制 SplashScreen
• Android 6–11:由 androidx.core:splashscreen 提供兼容行为