Android:如何在新的 SplashScreen API 中将可绘制对象设置为 windowSplashScreenBackground 参数?

Alm*_*KhR 30 android splash-screen android-theme

我想使用Splash Screen API制作带有渐变背景和徽标的启动屏幕

像这样:

在此输入图像描述

我对值是纯色的字段进行了样式设置windowSplashScreenBackground,效果很好:

<style name="Theme.MyApp.SplashScreen" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/blue</item>
    <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
    <item name="postSplashScreenTheme">@style/Theme.MyApp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

该属性windowSplashScreenBackground只能采用颜色。

有什么想法如何创建渐变背景吗?可能是这样的:

 <style name="Theme.MyApp.SplashScreen" parent="Theme.SplashScreen">
    ... 
    <item name="windowSplashScreenBackground">@drawable/gradient_splash_screen_background</item>
    ...
</style>
Run Code Online (Sandbox Code Playgroud)

更新

我还尝试在字段中声明背景android:windowBackground而不是windowSplashScreenBackground

<style name="Theme.MyApp.SplashScreen" parent="Theme.SplashScreen">
    <item name="android:windowBackground">@drawable/gradient_splash_screen_background</item>
<!--        <item name="windowSplashScreenBackground">@color/purple</item>-->
<!--        <item name="windowSplashScreenBackground">@drawable/splash_screen_layers</item>-->
    <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
    <item name="postSplashScreenTheme">@style/Theme.MyApp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

但它解决了一个问题并消耗了另一个问题。屏幕背景接受渐变但徽标变得不可见

yoz*_*hik 9

简短的答案是 - 不,您不能在 windowSplashScreenBackground 中使用任何可绘制对象。只有常规的十六进制颜色。这是新的闪屏 api 的限制。我在2022年6月1日使用并深入研究过这个版本:

"androidx.core:core-splashscreen:1.0.0-rc01"
Run Code Online (Sandbox Code Playgroud)

如果您像这样使用此属性,或者完全删除它 - 您将收到结果 - 背景上的默认系统颜色或 API 级别 < 31 上的全黑屏幕。

<item name="windowSplashScreenBackground">@null</item>
<item name="windowSplashScreenBackground">@drawable/some_custom_splash_background</item>
Run Code Online (Sandbox Code Playgroud)

与 icon 属性相同的故事:如果它是 NULL - 那么将显示默认应用程序图标(但在 android api < 31 - 情况会更糟),如果图标大小错误 - 它将被压缩为固定大小和圆圈形状。如果您完全删除该属性 - 它将使用默认的应用程序图标。你无法逃离屏幕中央的图标。Maby只是尝试用透明颜色制作这样的图标——我认为它会有所帮助,但我自己没有尝试过。

<item name="windowSplashScreenAnimatedIcon">@null</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
Run Code Online (Sandbox Code Playgroud)

其他属性,如:windowSplashScreenIconBackgroundColor、windowSplashScreenBrandingImage - 仅适用于 API 版本 31+。

我发现的解决方法是:在 Android 12+ 上使用 SplashScreenAPI 的新方法,在所有旧版本上使用旧方法。我有一个 MainActivity - 我的所有屏幕都是碎片。我有两个 style.xml 文件:res/values/style.xml 和 res-v31/values/style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.App.NewSplashScreenTheme" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">#24a0d1</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
        <item name="postSplashScreenTheme">@style/Theme.MyAppTheme</item>
        <item name="windowSplashScreenIconBackgroundColor">#f0e067</item>
        <item name="android:windowSplashScreenBrandingImage">@drawable/brand_logo</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

这是 style.xml ——适用于所有以前的 Android 版本:

<style name="Theme.App.NewSplashScreenTheme" parent="Theme.MyAppTheme">
        <item name="android:windowBackground">@drawable/splash_screen_background</item>
        <item name="android:windowFullscreen">true</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

然后在 MainActivity 的 onCreate 方法中我只检查 Android API 版本。就是这样。

override fun onCreate(savedInstanceState: Bundle?) {
    if (Build.VERSION.SDK_INT >= 31){
        val splashScreen = installSplashScreen() //init new splash screen api for Android 12+
    } else{
        setTheme(R.style.Theme_MyAppTheme) //else use old approach
    }
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    }
Run Code Online (Sandbox Code Playgroud)

在AndroidManifest中使用新的主题,它具有相同的名称,但对于不同的android版本来说是不同的:

<activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.App.NewSplashScreenTheme">
Run Code Online (Sandbox Code Playgroud)

但对我来说 - 我强烈建议仅使用一种方法:新的闪屏 api 或旧的方法。因为谁知道它在未来版本中的表现如何。

另外,我可以推荐一些阅读链接,这有助于我更快地调查它: https: //developer.android.com/guide/topics/ui/splash-screen https://developer.android.com/guide/topics /ui/启动画面/迁移

https://www.raywenderlich.com/32555180-splash-screen-tutorial-for-android https://itnext.io/a-compressive-guide-to-android-12s-splash-screen-api-644609c811fa https: //habr.com/ru/post/648535/ https://medium.com/viithiisys/android-perfect-way-to-create-splash-screen-ca3c5bee137f https://proandroiddev.com/splash-screen-android -12-173c3c641671 https://medium.com/nerd-for-tech/modern-splash-screen-in-android-9c804903c7c9


Cyr*_*obé 6

我与一位从事 Android 框架工作的 Google 工程师进行了讨论(在 Slack 小组“Android Developers France”上)。他告诉我这不是限制,而是设计使然。谷歌设计师希望统一设计以及操作系统和应用程序之间的转换。

(这是他的答案的屏幕截图 - 法语 - https://i.stack.imgur.com/jaiwy.jpg

他还表示,我们仍然可以使用闪屏编写自定义动画以淡入渐变。

希望能帮助到你。