Android 12 闪屏 API 定制

ahe*_*man 25 customization android splash-screen kotlin android-12

自从 Android 在 Android 12 中发布了新的 Splash Screen API 以来,许多应用程序都存在重复的启动屏幕、缺乏自定义等问题。

现在,可以设置背景颜色和中间的图标,但是可以自定义更多吗?由于现在我们只能使用单色背景和不可调整大小的徽标图标,这看起来不太好。

我想要实现的是一个自定义的启动屏幕,带有可绘制的图像作为背景(或包含 2 个项目的图层列表 - 一个背景图像和一个居中徽标),因为它可以在 Android 12 之前使用。

有人成功地实现了这种行为吗?

有一种解决方法可以将 windowIsTranslucent 属性设置为 true 并仅显示第二个启动画面(右侧的启动画面),但它会带来糟糕的用户体验,因为应用程序似乎有几秒钟没有响应。

Ami*_*ein 0

我做了这样的事情。首先删除默认的drawable

 <style name="Theme.App.Starting" parent="Theme.SplashScreen">
      <item name="windowSplashScreenBackground">@color/...</item>
      <item name="windowSplashScreenAnimatedIcon">@android:color/transparent</item>
      <item name="postSplashScreenTheme">@style/AppTheme</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后膨胀您的自定义启动视图

class MainActivity : AppCompatActivity(), SplashScreen.OnExitAnimationListener {

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          val installSplashScreen = installSplashScreen()
          installSplashScreen.setOnExitAnimationListener(this)
          setContentView(R.layout.activity_main)
      }


      override fun onSplashScreenExit(splashScreenViewProvider: SplashScreenViewProvider) {
           val view = splashScreenViewProvider.view
           if (view is ViewGroup) {
               val binding = ActivityMainSplashBinding.inflate(layoutInflater,view, true)
               // Do what you want with your inflated view
               animate(view) {
                   // Remove splash
                   splashScreenViewProvider.remove()
               }
           }
       }



     private fun animate(view: View, doOnFinish: () -> Unit) {
         view.animate()
        .withEndAction(doOnFinish)
        .start()
     }
}
Run Code Online (Sandbox Code Playgroud)

最后记得调用splashScreenViewProvider.remove()来删除splash。