SplashScreen is built into Android for displaying various boot effects, but Android versions below 12 are the most popular devices in the market. Google has also released a compatible version of SplashScreen in JetPack. The main content of this article is a straightforward guide to configuring SplashScreen on Android12 or later.

Importing a dependency library

Implementation (' androidx. Core: the core - splashscreen: 1.0.0 - alpha02 ')Copy the code

Configure the topic

Configure the theme used for the launch page

The Theme of the splash page must inherit from Theme.SplashScreen

<style name="Theme.TestSplashScreen.Starting" parent="Theme.SplashScreen">The background of the splash screen, using windowBackground by default<item name="windowSplashScreenBackground">@color/xieyi</item>Static drawable or animated vector drawable is supported<item name="windowSplashScreenAnimatedIcon">@drawable/logo</item># Animation icon duration, up to 1000 ms<item name="windowSplashScreenAnimationDuration">1000</item>The theme of the Activity after the launch screen exits<item name="postSplashScreenTheme">@style/MainTheme</item>
</style>
Copy the code
Configure the theme after the splash screen is complete
<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>
Copy the code
Set the theme for the activity

Want to which the Activity (typically start page) set the SplashScreen effect, you must set the Theme for the newly created Theme. TestSplashScreen. Starting the Theme, or have no effect.

<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>
Copy the code

Initialize the SplashScreen

SplashScreen initialization needs to be performed before setContentView in case the theme configuration is not found.

val splashScreen = installSplashScreen()
setContentView(R.layout.activity_splash)
Copy the code

Delay the startup screen duration

JetPack’s version of SplashScreen provides a dedicated API for controlling how long a screen is displayed. Why? By default, the SplashScreen Window on the launch page will exit when the first frame of the App is drawn, but if some logic is left unfinished, consistency and integrity may be affected in some cases. SplashScreen provides the KeepOnScreenCondition API to fulfill this requirement, which terminates the screen when a predetermined condition is reached.

companion object {
    const val DURATION = 2000
}

val splashScreen = installSplashScreen()
setContentView(R.layout.activity_splash)
compatDelay(splashScreen)
// Control the screen length
private fun compatDelay(splashScreen: SplashScreen) {
    splashScreen.setKeepVisibleCondition {
        (SystemClock.uptimeMillis() - initTime) < DURATION
    }
}
Copy the code

Custom exit animation

Since the compatible library only provides exit animation, only one exit animation is simply implemented here. This exit animation realizes the center icon moving slowly from the center of the interface, and the exit process is completed when the icon slides out of the interface. Of course, other animation effects such as fading in and out, zooming in and out can also be set.

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()

    }
})
Copy the code

Results show