Android 12 (API 31) introduces the SplashScreen API for developing the launch page for Android applications.
Affects all applications running on android 12 devices:
The introduction of splashscreen-related apis affects all applications running on Android 12 devices. For application developers, regardless of the targetSdkVersion of your application, SplashScreen adaptation is required.
SplashScreen adaptations must be made:
If the developer has not adapted SplashScreen, when the application is running on Android 12 or later, and the application is cold or warm startup, The Android system constructs a default startup animation (the default startup animation consists of the app ICON + the windowBackground content of the app theme). Therefore, for applications without SplashScreen API adaptation, two startup pages (Android SplashScreen startup page and customized startup or boot page for Android applications) may appear successively during cold startup and warm startup.
Let’s take a look at the official AndoridSplashScreen
Start the page running effect.
- SplashScreen usage mode
- SplashScreen Startup page composition
- SplashScreen use example source code download
First, use mode
According to the relevant official migration documents, I developed the following startup page operation effect.
Here’s an example of how to use the SplashScreen API:
- Build. Gradle adds dependencies.
- Custom SplashScreen themes;
- Custom launch page Activity;
1.1 build. Gradle
Build. The gradle:
- Change the version of compileSdkVersion.
- Introducing splashscreen-related dependencies;
Android {// change version of compileSdkVersion compileSdk 31} dependencies {// introduce splashscreen dependency implementation "Androidx. Core: the core - splashscreen: 1.0.0 - alpha02"}Copy the code
1.2 SplashScreen theme
Create a Theme with a parent Theme.SplashScreen and set the corresponding Theme to the launch page Activity application.
- Create a custom SplashScreen theme.
- The SplashScreen theme is applied to the corresponding Activity;
Create the SplashScreen theme:
Directly give the code example of style, all attributes in the theme are given a clear annotation, no repeat explanation. Here especially emphasize that windowSplashScreenAnimatedIcon this property, can be a picture, animation, animated – vector animation, etc.
<! Style -->
<style name="Theme.SplashScreen.Demo" parent="Theme.SplashScreen">
<! -- Splash screen background color -->
<item name="windowSplashScreenBackground">@color/splashscreen_bg</item>
<! Here can be a picture, frame animation, etc. -->
<item name="windowSplashScreenAnimatedIcon">@drawable/splash_anim_icon</item>
<item name="windowSplashScreenIconBackgroundColor">@color/splashscreen_icon_bg</item>
<! -- How long icon animation will be displayed before closing: Max: 1000 ms -->
<item name="windowSplashScreenAnimationDuration">1000</item>
<! -- Brand image at the bottom of the splash screen -->
<item name="android:windowSplashScreenBrandingImage">@drawable/brand_img</item>
<! -- Splash's exit theme -->
<item name="postSplashScreenTheme">@style/Theme.Android12_Splash</item>
</style>
Copy the code
The SplashScreen theme is applied to the start page Activity
I’ll customize the launch page as SplashActivity.
<activity
android:name=".SplashActivity"
android:exported="true"
android:theme="@style/Theme.SplashScreen.Demo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Copy the code
1.3 Start the Activity page
As mentioned above, the launch page Activity, I custom defined as SplashActivity, source code is as follows:
class SplashActivity : AppCompatActivity() {
val TAG: String = "CoroutineScope"
/ / coroutines (understand the use of coroutines, may refer to: https://xiaxl.blog.csdn.net/article/details/123383727).
lateinit var mCoroutineScope: CoroutineScope
/ / data
private var mKeepOnAtomicBool = AtomicBoolean(true)
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
Log.d(TAG, "----onCreate----")
// Initialize operation (must precede setContentView())
val mSplashScreen = installSplashScreen()
// setContentView (can be omitted)
setContentView(R.layout.activity_splash)
// Before each UI drawing, it will determine whether Splash is necessary to continue to display on the screen; Hide the Splash until the condition is no longer met.mSplashScreen!! .setKeepVisibleCondition(object : SplashScreen.KeepOnScreenCondition {
override fun shouldKeepOnScreen(a): Boolean {
return mKeepOnAtomicBool.get()}})// Splash displays the finished listening methodmSplashScreen!! .setOnExitAnimationListener(object : SplashScreen.OnExitAnimationListener {
override fun onSplashScreenExit(splashScreenViewProvider: SplashScreenViewProvider) {
startSplashScreenExit(splashScreenViewProvider)
}
})
// Create CoroutineScope (to manage all coroutines in CoroutineScope)
mCoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
mCoroutineScope.launch(Dispatchers.IO) {
Log.d(TAG, "----launch----")
// TODO async thread
// Splash displays for 2 seconds
delay(2000)
// Splash displays
mKeepOnAtomicBool.compareAndSet(true.false)}}/** * onDestroy */
override fun onDestroy(a) {
super.onDestroy()
Log.d(TAG, "----onDestroy----")
Cancel all coroutines managed by the Scope when the Activity is destroyed.
mCoroutineScope.cancel()
}
/** * SplashScreen to exit */
private fun startSplashScreenExit(splashScreenViewProvider: SplashScreenViewProvider) {
Log.d(TAG, "----onSplashScreenExit----")
// splashScreenView
val splashScreenView = splashScreenViewProvider.view
// splashIconView
val iconView = splashScreenViewProvider.iconView
/** * ScreenView Alpha animation */
val splashAlphaAnim = ObjectAnimator.ofFloat(splashScreenView, View.ALPHA, 1f.0f)
splashAlphaAnim.duration = 500
splashAlphaAnim.interpolator = FastOutLinearInInterpolator()
/** * iconView moves down animation */
val translationY = ObjectAnimator.ofFloat(
iconView,
View.TRANSLATION_Y,
iconView.translationY,
splashScreenView.height.toFloat()
)
translationY.duration = 500
translationY.interpolator = FastOutLinearInInterpolator()
// Merge gradient animation & Drop animation
val animatorSet = AnimatorSet()
animatorSet.playTogether(translationY, splashAlphaAnim)
// The method called when the animation ends
animatorSet.doOnEnd { onAnimEnd(splashScreenViewProvider) }
// Start animation
animatorSet.start()
}
/** ** when the animation ends */
private fun onAnimEnd(splashScreenViewProvider: SplashScreenViewProvider) {
// Remove the listener
splashScreenViewProvider.remove()
// Go to the next page
// Go to the main screen
Log.d(TAG, "----startActivity MainActivity----")
startActivity(Intent(this@SplashActivity, MainActivity::class.java))
this@SplashActivity.finish()
Log.d(TAG, "----SplashActivity finish----")
// The Activity exits
overridePendingTransition(0, R.anim.activity_out)
}
}
Copy the code
2. SplashScreen
Again, the code effect of this example is used as an example.
- SplashScreen constitute
- Size of SplashScreen center ICON
SplashScreen constitute
The SplashScreen Theme is the same as the SplashScreen Theme.
<! Style -->
<style name="Theme.SplashScreen.Demo" parent="Theme.SplashScreen">
<! -- Splash screen background color -->
<item name="windowSplashScreenBackground">@color/splashscreen_bg</item>
<! Here can be a picture, frame animation, etc. -->
<item name="windowSplashScreenAnimatedIcon">@drawable/splash_anim_icon</item>
<item name="windowSplashScreenIconBackgroundColor">@color/splashscreen_icon_bg</item>
<! -- How long icon animation will be displayed before closing: Max: 1000 ms -->
<item name="windowSplashScreenAnimationDuration">1000</item>
<! -- Brand image at the bottom of the splash screen -->
<item name="android:windowSplashScreenBrandingImage">@drawable/brand_img</item>
<! -- Splash's exit theme -->
<item name="postSplashScreenTheme">@style/Theme.Android12_Splash</item>
</style>
Copy the code
The corresponding attribute position is shown in the figure below:
SplashScreen center ICON size
The dp size of the SplashScreen center ICON is as follows:
Third, the case source code
This case SplashScreen case source code download address as follows: download.csdn.net/download/ai…
reference
Android will splash screen migrated to the SplashScreen: developer. The Android, Google. Cn/guide/topic…
Android12 SplashScreen: developer. The android. Google. Cn/about/versi…