preface

1. What is Android’s cold boot time?

The cold start time is the time between the moment you click the desktop LOGO and the time before the Activity on the launch page calls onCreate().

2. What happens during the cold start period?

When an Activity is started, if the Application is not already started, the system creates a process. The creation and initialization of the process takes some time. During this time, WindowManager loads the windowBackground in the APP’s theme style as a preview element before loading the actual layout. If this takes too long, the user will get the impression that the APP is not running smoothly

Common practice

  • Set the background to the APP Logo, which is what most apps do in the market
<style name="AppTheme" parent="BaseTheme">
    <item name="android:windowBackground">@drawable/window_splash_screen_content</item>
</style>
Copy the code
  • Set the background to a transparent color. When clicking on the desktop LOGO, the APP will not start immediately. Instead, the APP will stay on the desktop for a while.
<style name="AppTheme" parent="BaseTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>
Copy the code

The above method can achieve the second to open the effect, but belongs to cover one’s ears and steal a bell.

The Android 8.0

Google used to discourage the use of splash screens, but now that many apps are using them, Google wants to make it easier to create a startup screen.

The Splash Screen API is available in Android Oreo, which allows you to set a drawable resource to a Splash Screen.

Create a directory with values-v26:

<resources>
    <style name="AppTheme" parent="BaseTheme">
        <item name="android:windowSplashscreenContent">@drawable/window_splash_screen_content</item>
    </style>
</resources>
Copy the code

Through setting the windowSplashscreenContent drawable resources will cover on the top of the windowBackground, under the System status bar, if you don’t want to is limited by System Bars, please use the full screen theme.

If not set windowSplashscreenContent, the system will use other properties to display the splash screen pages, such as: windowBackground.

disadvantages

  • Unable to define animation
  • Uncontrollable duration

WindowBackground sets the timing

Take <Android API 30 Platform> as an example

PhoneWindowManager

public StartingSurface addSplashScreen(IBinder appToken, String packageName, int theme,
        CompatibilityInfo compatInfo, CharSequence nonLocalizedLabel, int labelRes, int icon,
        int logo, int windowFlags, Configuration overrideConfig, int displayId) {...try{...final PhoneWindow win = new PhoneWindow(context);
        win.setIsStartingWindow(true); win.setType(WindowManager.LayoutParams.TYPE_APPLICATION_STARTING); .finalWindowManager.LayoutParams params = win.getAttributes(); . addSplashscreenContent(win, context); wm = (WindowManager) context.getSystemService(WINDOW_SERVICE); view = win.getDecorView(); wm.addView(view, params);// Only return the view if it was successfully added to the
        // window manager... which we can tell by it having a parent.
        returnview.getParent() ! =null ? new SplashScreenSurface(view, appToken) : null; }...return null;
}

private void addSplashscreenContent(PhoneWindow win, Context ctx) {
    final TypedArray a = ctx.obtainStyledAttributes(R.styleable.Window);
    final int resId = a.getResourceId(R.styleable.Window_windowSplashscreenContent, 0);
    a.recycle();
    if (resId == 0) {
        return;
    }
    final Drawable drawable = ctx.getDrawable(resId);
    if (drawable == null) {
        return;
    }
    // We wrap this into a view so the system insets get applied to the drawable.
    final View v = new View(ctx);
    v.setBackground(drawable);
    win.setContentView(v);
}
Copy the code

Android 12

Android12 adds the new SplashScreen API, which enables new splash screens for all apps, including app movement when launched, splash screens showing app ICONS, and transitions to the app itself.

The working principle of

When the user starts the application and the application process is not running (cold start) or the Activity has not been created (warm start) :

  • The system uses themes and custom animations to display the splash screen
  • When the application is ready, the splash screen closes and the application is displayed

Hot boot does not display a splash screen.

Animation elements and mechanics

Consists of a window background, an animated application icon, and an icon background:

  • The application icon (1) should be a vector drawable object, or it can be static or animated. The animation icon will automatically play when the splash screen is displayed. The duration of the animation icon is unlimited, but it is recommended that the icon Launcher be used for no longer than 1000 milliseconds
  • Icon background (2) is optional and useful when a higher contrast between the icon and the window background is required
  • The window background (4) consists of an opaque monochrome

Splash screen animation consists of enter animation and exit animation:

  • Enter animation by system control, not self – defined
  • Exit animation can be customized, with access to SplashScreenView and ICON. You can set any animation (displacement, transparency, color, etc.). You need to manually remove the splash screen when the animation is finished

Custom splash screen

Create the values-v31 directory

<resources>
    <style name="AppTheme" parent="BaseTheme">
        <! -- Monochrome fill background -->
        <item name="android:windowSplashScreenBackground">#F18C1A</item>
        <! -- replace ICON -->
        <item name="android:windowSplashScreenAnimatedIcon">@mipmap/ic_loading_logo</item>
        <! Set how long the splash screen will be displayed before closing. Max time: 1000ms -->
        <item name="android:windowSplashScreenAnimationDuration">1000</item>
        <! -- Set the background behind the splash screen icon -->
        <item name="android:windowSplashScreenIconBackgroundColor">#ffffff</item>
        <! Set the picture to be displayed at the bottom of the splash screen. Design guidelines advise against using brand images. -->
        <item name="android:windowSplashScreenBrandingImage">
            @drawable/window_splash_screen_content
        </item>
    </style>
</resources>
Copy the code

Make the splash screen stay on the screen longer

The splash screen closes immediately after the application draws the first frame. If you need the asynchronous loading a small amount of data, you can use the ViewTreeObserver. The application is stopped OnPreDrawListener draw the first frame.

companion object {
    const val DURATION = 2000
}
private val initTime = SystemClock.uptimeMillis()

val content: View = findViewById(android.R.id.content)
content.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
    override fun onPreDraw(a): Boolean {
        return if ((SystemClock.uptimeMillis() - initTime) > DURATION) {
            content.viewTreeObserver.removeOnPreDrawListener(this)
            true
        } else false}})Copy the code

Custom exit animation

Exit animation can be defined by getSplashScreen()

splashScreen.setOnExitAnimationListener { splashScreenView ->
    ...
}
Copy the code

Narrow exit to center

@RequiresApi(Build.VERSION_CODES.S)
private fun scaleExit(view: SplashScreenView) {
    ObjectAnimator.ofFloat(view, View.SCALE_X, View.SCALE_Y, Path().apply {
        moveTo(1f.1f)
        lineTo(0f.0f)
    }).apply {
        doOnEnd { view.remove() }
        start()
    }
}
Copy the code

ICON to exit

@RequiresApi(Build.VERSION_CODES.S)
private fun slideUp(view: SplashScreenView) {
    valiconView = view.iconView ? :return
    AnimatorSet().apply {
        playSequentially(
            ObjectAnimator.ofFloat(iconView, View.TRANSLATION_Y, 0f.50f),
            ObjectAnimator.ofFloat(iconView, View.TRANSLATION_Y, 50f, -view.height.toFloat()),
        )
        doOnEnd { view.remove() }
        start()
    }
}
Copy the code