Android 12 启动画面 API 定制

Posted

技术标签:

【中文标题】Android 12 启动画面 API 定制【英文标题】:Android 12 Splash Screen API customization 【发布时间】:2021-10-30 11:32:34 【问题描述】:

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

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

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

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

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

【问题讨论】:

大声笑,点赞,但没有答案:( @Xam 是的,他们似乎希望它是统一的,没有干净的方法可以打破这些限制,AFAIK.. 【参考方案1】:

我做了这样的事情。 首先删除默认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>

然后扩充您的自定义初始视图

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

最后记得调用 splashScreenViewProvider.remove() 来移除飞溅。

【讨论】:

以上是关于Android 12 启动画面 API 定制的主要内容,如果未能解决你的问题,请参考以下文章

Android 12 启动画面-SplashScreen

Android 12 启动画面-SplashScreen

Android 12 启动画面设置KeepVisibleCondition

Android 12 新APP启动画面(SplashScreen API)简介&&源码分析

从 Jetpack SplashScreen 深度探讨 App 启动画面的前世今生~

Android上的启动画面无法正常工作