Android12以下配置JetPack闪屏页SplashScreen

SplashScreen是Android内置的用于展示各种启动特效的功能,但是目前Android12以下的版本才是市场的主流设备,针对这种情况Google也在JetPack中推出了兼容版本的SplashScreen,本文的主要内容就是简单明了的介绍一下如何在Android12以下版本中配置SplashScreen。

导入依赖库

implementation('androidx.core:core-splashscreen:1.0.0-alpha02')
复制代码

配置主题

配置启动页面使用的主题

闪屏页的主题必须继承自Theme.SplashScreen

<style name="Theme.TestSplashScreen.Starting" parent="Theme.SplashScreen">
    # 启动画面的背景,默认使用 windowBackground
    <item name="windowSplashScreenBackground">@color/xieyi</item>
    # 指定 icon,支持静态 drawable 或动画 vector drawable
    <item name="windowSplashScreenAnimatedIcon">@drawable/logo</item>
    # 动画 icon 时长,上限 1000 ms
    <item name="windowSplashScreenAnimationDuration">1000</item>
    # 启动画面退出后 Activity 的主题
    <item name="postSplashScreenTheme">@style/MainTheme</item>
</style>
复制代码
配置闪屏结束之后的主题
<style name="MainTheme" parent="Theme.MvvmWanAndroid">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

<style name="Theme.MvvmWanAndroid.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
复制代码
为activity设置主题

想要为哪个Activity(一般是启动页)设置SplashScreen效果,就必须将其主题设置为刚刚创建的Theme.TestSplashScreen.Starting主题,否则没有效果。

<activity
    android:name=".splash.SplashActivity"
    android:exported="true"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.TestSplashScreen.Starting">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
复制代码

初始化SplashScreen

SplashScreen的初始化过程需要放到setContentView之前执行,防止出现找不到主题配置的情况。

val splashScreen = installSplashScreen()
setContentView(R.layout.activity_splash)
复制代码

延迟启动画面时长

JetPack版本的SplashScreen提供了专门的API来控制画面的展示时长,为什么要控制时长呢?默认情况下当App的第一帧开始描画的时候启动页的SplashScreen Window就要退出了,但是如果有一些逻辑没有还没有执行完,某些情况下可能会影响连贯性和完整性。SplashScreen提供了KeepOnScreenCondition API来实现这种需求,当达到预定条件之后才会结束画面。

companion object {
    const val DURATION = 2000
}

val splashScreen = installSplashScreen()
setContentView(R.layout.activity_splash)
compatDelay(splashScreen)
//控制画面时长
private fun compatDelay(splashScreen: SplashScreen) {
    splashScreen.setKeepVisibleCondition {
        (SystemClock.uptimeMillis() - initTime) < DURATION
    }
}
复制代码

定制退场动画

由于兼容库只提供了退场动画,所以这里只简单实现一个退场动画,这个退场动画实现中心图标从界面中心向上慢慢移动,当图标滑动出界面时完成退场过程,当然也可以设置淡入淡出、缩放等其他动画效果。

splashScreen.setOnExitAnimationListener(SplashScreen.OnExitAnimationListener { provider ->
    val iconView = provider.iconView
    AnimatorSet().apply {
        playSequentially(
            ObjectAnimator.ofFloat(iconView, View.TRANSLATION_Y, 0f, 50f),
            ObjectAnimator.ofFloat(
                iconView,
                View.TRANSLATION_Y,
                50f,
                -provider.view.height.toFloat()
            ),
        )
        doOnEnd {
            provider.remove()
        }
        start()

    }
})
复制代码

效果展示

anhnr-pu2v2.gif

猜你喜欢

转载自juejin.im/post/7032856371565592607