Android 12 introduces the SplashScreen API which enables a new app launch animation.
Implementation in Android app
We should follow the below points at the time of implementing SplashScreen:-
- We should use vector drawable for app icon(1). The app icon can either static or animated and the animation may have unlimited time, however it should not exceed 1000 miliseconds. By default, the launcher icon is used.
- The icon background (2) is optional and it is useful if more contrast is needed between the icon and the window background.
- As with adaptive icons, ⅓ of the foreground is masked (3).
- The window background (4) consists of a single opaque color.
Note:- The splash screen animation mechanics consist of entering and exit animations.
Enter animation
The enter animation contains the android OS view to the splash screen. The android OS control it and is not customizable.
Exit animation
The exit animation contains the animation run that hides the splash screen. If we want to customize it, we should have access to SplashScreenView.
More about SplashScreenView:-
Customize the splash screen
For customizing the SplashScreen
- Setting theme attributes to change its appearance
- Keeping it on-screen for a longer period
- Customizing the animation for dismissing the splash screen
Set a theme for the splash screen to change its appearance
If we are already using attributes like android:windowBackground, then subsequently we can also use an alternate resource file for Android 12.
1 2 |
<item name="android:windowSplashScreenBackground">@color/...</item> <item name="android:windowSplashScreenAnimatedIcon">@drawable/...</item> |
1 2 |
<item name=”android:windowSplashScreenIconBackground”>@color/...</item> <item name=”android:windowSplashScreenBrandingImage”>@drawable/...</item> |
Keep the splash screen on-screen for longer periods
When the app draws the first frame, the splashscreen will disappear. If we need to load a small amount of data from a local disk asynchronously, we can use ViewTreeObserver.OnPreDrawListener to suspend the app to draw its first frame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Create a new event for the activity. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Set the layout for the content view. setContentView(R.layout.main_activity) // Set up an OnPreDrawListener to the root view. val content: View = findViewById(android.R.id.content) content.viewTreeObserver.addOnPreDrawListener( object : ViewTreeObserver.OnPreDrawListener { override fun onPreDraw(): Boolean { // Check if the initial data is ready. return if (viewModel.isReady) { // The content is ready; start drawing. content.viewTreeObserver.removeOnPreDrawListener(this) true } else { // The content is not ready; suspend. false } } } ) } |
Customize the animation for dismissing the splash screen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... // Add a callback that's called when the splash screen is animating to // the app content. splashScreen.setOnExitAnimationListener { splashScreenView -> // Create your custom animation. val slideUp = ObjectAnimator.ofFloat( splashScreenView, View.TRANSLATION_Y, 0f, -splashScreenView.height.toFloat() ) slideUp.interpolator = AnticipateInterpolator() slideUp.duration = 200L // Call SplashScreenView.remove at the end of your custom animation. slideUp.doOnEnd { splashScreenView.remove() } // Run your animation. slideUp.start() } } |
Please go through the below links to get more details on Android 12 SplashScreen API:-