如何使用Google推荐的Splash Screen API在Android 12闪屏页展示Lottie animation
基于Android官方Splash Screen API实现Lottie闪屏动画的方案
Android 12+ 原生系统闪屏API默认仅支持静态图标、矢量图及AnimatedVectorDrawable格式动画,不直接兼容Lottie格式。可以通过「系统闪屏退场后无缝衔接自定义Lottie动画」的方式实现需求,完全符合Google官方适配规范,不会出现白屏、跳帧等体验问题。
1. 引入基础依赖
在模块级build.gradle中添加官方闪屏兼容库和Lottie依赖:
// 官方Splash Screen兼容库,支持Android 5.0+设备统一适配闪屏逻辑 implementation "androidx.core:core-splashscreen:1.0.1" // Lottie动画官方依赖 implementation "com.airbnb.android:lottie:6.1.0"
2. 配置系统闪屏基础主题
2.1 新建v31版本主题
在res/values-v31/styles.xml中配置系统闪屏基础样式,注意所有颜色配置要和后续Lottie页面的配置完全一致,避免出现色差:
<style name="AppSplashTheme" parent="Theme.SplashScreen"> <!-- 系统闪屏背景色,和Lottie页面背景色保持一致 --> <item name="windowSplashScreenBackground">@color/white</item> <!-- 这里填和Lottie动画首帧一致的静态占位图标,也可以填透明图避免进场突兀 --> <item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash_placeholder</item> <!-- 闪屏退场后生效的App主主题 --> <item name="postSplashScreenTheme">@style/AppTheme</item> </style>
2.2 Manifest中绑定主题
给启动页Activity绑定上述闪屏主题:
<activity android:name=".SplashActivity" android:exported="true" android:theme="@style/AppSplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
3. 实现Lottie闪屏布局
新建activity_splash.xml,背景色和之前配置的系统闪屏背景色完全一致:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <com.airbnb.lottie.LottieAnimationView android:id="@+id/lottie_view" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" app:lottie_rawRes="@raw/splash_anim" <!-- 你的Lottie动画文件放在res/raw目录下 --> app:lottie_autoPlay="false" app:lottie_loop="false"/> </FrameLayout>
4. 实现启动页逻辑,无缝衔接系统闪屏与Lottie动画
在SplashActivity中自行控制系统闪屏的退场时机,等待Lottie加载完成后再播放动画,动画结束后跳转主页面:
class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { // 必须在super.onCreate之前调用,安装系统闪屏能力 val splashScreen = installSplashScreen() super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) // 禁用系统闪屏自动退场,由我们自己控制退场时机 splashScreen.setKeepOnScreenCondition { true } val lottieView = findViewById<LottieAnimationView>(R.id.lottie_view) // 监听Lottie动画加载完成后再启动播放 lottieView.addLottieOnCompositionLoadedListener { lottieView.playAnimation() } // 监听Lottie动画播放完成,跳转主页面 lottieView.addAnimatorListener(object : Animator.AnimatorListener { override fun onAnimationStart(animation: Animator) = Unit override fun onAnimationCancel(animation: Animator) = Unit override fun onAnimationRepeat(animation: Animator) = Unit override fun onAnimationEnd(animation: Animator) { startActivity(Intent(this@SplashActivity, MainActivity::class.java)) finish() } }) } }
注意事项
- 系统闪屏的占位图标建议和Lottie动画首帧完全一致,加上背景色统一,用户完全感知不到系统闪屏到自定义Lottie页面的切换
- 该方案兼容Android 5.0以上所有设备,低版本设备会由兼容库自动模拟系统闪屏效果
- Lottie动画文件建议控制在2MB以内,避免出现加载卡顿
内容的提问来源于stack exchange,提问作者vinay kumar
相关产品推荐
相关产品推荐

