Android 12 SplashScreen 应用启动画面

从 Android 12 开始,在所有应用的冷启动和温启动期间,系统一律会应用 Android 系统的默认启动画面。默认情况下,此系统默认启动画面由应用的启动器图标元素和主题的 windowBackground(如果是单色)构成。
注:实际测试发现如果 windowBackground 为一张图片,则会从这种图里提取出单色。

假如有一个 SplashActivity 使用了这样的主题(img_splash 为一张有图片文字的图):

    <style name="Theme.SplashTest.Splash" parent="Theme.SplashTest">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@drawable/img_splash</item>
    </style>

在 Android 10 上冷启动:
(传不了 gif,后续补图)

在 Android 12 上冷启动(可以看到没有显示背景图,而是显示从背景图里提取出的单色):
(传不了 gif,后续补图)

根据 SplashScreen compat 库推荐的迁移方案更新代码:

android {
   compileSdkVersion 31
   ...
}
dependencies {
   ...
   implementation 'androidx.core:core-splashscreen:1.0.0-alpha01'
}
    <style name="Theme.SplashTest.Starting" parent="Theme.SplashScreen">
        // Set the splash screen background, animated icon, and animation duration.
        <item name="windowSplashScreenBackground">@color/purple_500</item>

        // Use windowSplashScreenAnimatedIcon to add either a drawable or an
        // animated drawable. One of these is required.
        <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
        <item name="windowSplashScreenAnimationDuration">200</item>  # Required for
        # animated icons

        // Set the theme of the Activity that directly follows your splash screen.
        <item name="postSplashScreenTheme">@style/Theme.SplashTest.Splash</item>  # Required.
    </style>
    ```
将 SplashActivity 的 theme 由 Theme.SplashScreen 改为 Theme.SplashTest.Starting。
```kotlin
    override fun onCreate(savedInstanceState: Bundle?) {
        // Handle the splash screen transition.
        val splashScreen = installSplashScreen()

        super.onCreate(savedInstanceState)

        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ...
    }

在启动 activity 中,先调用 installSplashScreen,然后再调用 setContentView。

splashScreen.setKeepVisibleCondition {
    // e.g: return true until config is loaded
}

可以对 SplashScreen 设置保持可见的条件,默认是一直返回 false。

splashScreen.setOnExitAnimationListener {
    ...
}

还可以对 SplashScreen 设置退出监听器,默认是 null。

在 Android 10 上冷启动:
(传不了 gif,后续补图)

在 Android 12 上冷启动:
(传不了 gif,后续补图)

可以看到启动画面外观和风格是一致的。

缺点:

  • 在 Android 12 之前的系统上,闪屏图 不支持 Animated Vector Drawable 动画(测试发现在 Android 12 上也不支持,可能是 bug …)。
    Prior API 31, the platform behavior is replicated with the exception of the Animated Vector Drawable support on the launch screen.
    (其实目前测试在 Android 12 上也显示不了 AVD 动画。。。)
  • 无法对 闪屏图 设置点击监听以跳转到指定页面。

原理:
本质上是先将 Activity 的 Theme 设置为 Theme.SplashScreen,启动后再设置回 postSplashScreenTheme。

        open fun install() {
            ...
            setPostSplashScreenTheme(currentTheme, typedValue)
        }

        protected fun setPostSplashScreenTheme(
            currentTheme: Resources.Theme,
            typedValue: TypedValue
        ) {
            if (currentTheme.resolveAttribute(R.attr.postSplashScreenTheme, typedValue, true)) {
                finalThemeId = typedValue.resourceId
                if (finalThemeId != 0) {
                    activity.setTheme(finalThemeId)
                }
            }
        }

设置 splashScreen.setKeepVisibleCondition 是延迟 Activity 设置回 postSplashScreenTheme 后的第一帧绘制。

        open fun setKeepVisibleCondition(keepOnScreenCondition: KeepOnScreenCondition) {
            splashScreenWaitPredicate = keepOnScreenCondition
            val contentView = activity.findViewById<View>(android.R.id.content)
            val observer = contentView.viewTreeObserver
            observer.addOnPreDrawListener(object : OnPreDrawListener {
                override fun onPreDraw(): Boolean {
                    if (splashScreenWaitPredicate.shouldKeepOnScreen()) {
                        return false
                    }
                    contentView.viewTreeObserver.removeOnPreDrawListener(this)
                    mSplashScreenViewProvider?.let(::dispatchOnExitAnimation)
                    return true
                }
            })
        }

结论:
由于 windowSplashScreenAnimatedIcon 支持设置 闪屏图 或者 闪屏动画,以及 splashScreen.setKeepVisibleCondition 支持条件等待退出,实际上可以移除专有的 SplashActivity,将 splashScreen 应用在业务主 Activity 上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 12中的splashscreen启动画面)是一种应用程序启动时显示的短暂界面,用于提供应用程序的品牌标识和加载过程的视觉反馈。与以往的版本不同,Android 12引入了一些新的特性和改进来提升splashscreen的用户体验。 首先,Android 12splashscreen支持更多的自定义选项。开发者可以选择使用单一的图片、动画或者视频作为splashscreen的背景,以展示应用程序的标志性元素或者品牌内容。这增强了应用程序的品牌识别度,并为用户提供了更具吸引力的界面。 其次,Android 12还支持在splashscreen上显示应用程序的加载进度。开发者可以通过进度指示器或动画来告知用户应用程序的加载进度,让用户对应用程序的启动过程有更清晰的认知。这种视觉反馈可以提高用户对应用程序的满意度,并为用户提供更好的等待体验。 此外,Android 12还增加了一个预热阶段(pre-warming)来提升splashscreen的加载速度。当用户点击应用程序图标时,系统会在显示splashscreen之前暂时加载应用程序的一部分数据和资源,以缩短splashscreen的显示时间和启动过程的等待时间。这使得用户可以更快地进入应用程序,提高了用户体验的流畅度。 综上所述,Android 12splashscreen通过自定义选项、加载进度显示和预热阶段等特性和改进,提供了更具吸引力、更好的等待体验和更高的启动速度。这使得应用程序能够更好地展示自己的品牌标识,同时提升用户对应用程序的满意度和流畅度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值