Jetpack SplashScreen快速开屏

1,867 阅读1分钟

概述

android12新增了一个SplashScreen的启动页辅助类。不过他并不能向下兼容,只能在android12进行使用。因此官方在jetpack中开发了可以向下进行兼容的jetpack版SplashScreen(目前仍然是alpha所以还不不建议在项目中使用)。本文会简单讲讲jetpack版SplashScreen的使用。

简易使用

引入依赖

implementation 'androidx.core:core-splashscreen:1.0.0-alpha02'

配置主题

<style name="AnananSplashScreen" parent="Theme.SplashScreen">
        # 启动画面的背景,默认使用 windowBackground
        <item name="windowSplashScreenBackground">#d73</item>
        # 指定 icon,支持静态 drawable 或动画 vector drawable
        <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
        # 动画 icon 时长,上限 1000 ms
        <item name="windowSplashScreenAnimationDuration">1000</item>
        # 启动画面退出后 Activity 的主题
        <item name="postSplashScreenTheme">@style/Theme.SplashScreenSample</item>
        # 设置图标北京颜色,本例中图标是不透明的,所以看不到效果
        <item name="windowSplashScreenIconBackgroundColor">#f00</item>
    </style>

在manifest中配置activity主题

 <activity
            android:name=".MainActivity"
            android:theme="@style/AnananSplashScreen"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Activity中配置SplashScreen代码

只需要一行代码,安装jetpack版的SplashScreen即可使用

installSplashScreen()

延长展示时间

setKeepVisibleCondition方法可以延长显示时间

 private fun initSplashScreen() {
        var startMillis = SystemClock.uptimeMillis()
        val mSplashScreenView = installSplashScreen()
        mSplashScreenView.setKeepVisibleCondition {
            SystemClock.uptimeMillis() - startMillis < 1000 * 3
        }
    }

效果图

SplashScreen.gif

属性说明

windowSplashScreenAnimatedIcon

中心图标

windowSplashScreenBackground

整屏的背景颜色

windowSplashScreenAnimationDuration

动画持续时间,最长1s

windowSplashScreenIconBackgroundColor

中间图标背景颜色

git:github.com/ananananzhu…