Android 12 SplashScreen启动屏

SplashScreen是 Android 12 引入的特性,为了统一启动屏和优化启动,增加了 纯色背景+自适应图标 的启动屏。

一、启动图设计

启动页,由以下组成:

  1. 图标(矢量图)
  2. 图标背景(颜色)
  3. 图标遮罩层
  4. 窗口背景(颜色)

上图中 1-2 屏是 无图标背景的显示效果;3-4 屏是 有图标背景色的显示效果。

样式尺寸
类型 大小 说明
应用图标 240x240dp 内容在160dp圆内
应用图标(无图标背景) 288x288dp 内容在192dp圆内
品牌图片 200x80dp -

图标内容为 2/3 大小,图标遮罩(盖)层为 1/3 大小。

二、使用

1. 这里直接使用 兼容库
gradle 复制代码
dependencies {
    implementation "androidx.core:core-splashscreen:1.0.0"
}
2. 主题配置启动屏

添加启动屏主题,设置到 <activity />android:theme 属性上。

主题继承 Theme.SplashScreenTheme.SplashScreen.IconBackground,区别是:

  1. Theme.SplashScreen 没有图标背景,图标大小288dp,
  2. Theme.SplashScreen.IconBackground 设置图标背景色,图标240dp
xml 复制代码
<style name="Theme.App.Splash" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">#FF0000</item>
    <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_round</item>
    <item name="windowSplashScreenAnimationDuration">280</item>
    <item name="postSplashScreenTheme">@style/Theme.App</item>
</style>

主题属性:

  1. windowSplashScreenBackground 设置背景色或图片
  2. windowSplashScreenAnimatedIcon 设置中间的 图标
  3. windowSplashScreenAnimationDuration 中间 动画矢量图标 的时间
  4. postSplashScreenTheme 设置启动完成后的 主题,即 Activity 实际主题
3. 添加启动屏到Activity

调用 installSplashScreen() 设置 启动屏,然后 监听启动屏结束
splashScreen.setOnExitAnimationListener

注意:

  1. 默认自动 移除 启动屏;
  2. 设置setOnExitAnimationListener时,需要手动 splashScreenViewProvider.remove() 移除 启动屏。
kotlin 复制代码
class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val splashScreen = installSplashScreen()
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        splashScreen.setOnExitAnimationListener(object : SplashScreen.OnExitAnimationListener {
            override fun onSplashScreenExit(splashScreenViewProvider: SplashScreenViewProvider) {
                splashScreenViewProvider.remove()
                // 启动屏结束后,跳转页面
                navToMain()
                finish()
            }
        })
    }

    private fun navToMain() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
    }
}
4. 保持启动屏显示

通常 启动页 显示完后,可能APP并没有完全初始化。此时 可能需要等待,为了 保持 启动页 显示,可通过 OnPrewDrawListener 配置 false 保持 第一帧,不再绘制。

或直接使用 splashScreen.setKeepOnScreenCondition(),更简单地 保持和取消 启动屏显示。

kotlin 复制代码
val isAppInited = false

splashScreen.setKeepOnScreenCondition {
  !isAppInited
}

说明:

上述方法,内部也是使用 ViewTreeObserveraddOnPreDrawListenerremoveOnPreDrawListener 方法,

来 保持第一帧的显示 和 继续绘制。

直到 Condition 返回 false 时,才会触发 setOnExitAnimationListener 的回调。

文档

相关推荐
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab2 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿2 天前
Android MediaPlayer 笔记
android
Jony_2 天前
Android 启动优化方案
android
阿巴斯甜2 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇2 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android