我的启动画面为什么只在一些设备上有效?

huangapple go评论79阅读模式
英文:

Why is my splash screen work only on some devices?

问题

这是我期望的启动画面:

我的启动画面为什么只在一些设备上有效?

这是我实现的方式:

  1. android/app/src/main/res/drawable 目录下添加了启动画面的图片 (launch_image.png) 和渐变背景的 XML 文件 (gradient_background.xml)
  2. android/app/src/main/res/values 创建了一个名为 colors.xml 的文件,其中包含了在启动画面中显示的颜色:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name='gradientStartColor'>#68CDF6</color>
    <color name='gradientEndColor'>#80D325</color>
</resources>
  1. 将以下行添加到 gradient_background.xml 中:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="@color/gradientStartColor"
        android:endColor="@color/gradientEndColor"
        android:angle="135" />    
</shape>
  1. 最后,在 launch_background.xml 中添加了以下行:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/gradient_background"/> 

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/launch_image" />
    </item>

</layer-list>

由于存在两个名为 drawabledrawable-v21 的文件夹,我将第一个文件夹的内容复制到了第二个文件夹中,因为我实际上不知道第二个文件夹的用途。这个启动画面在我的物理设备上(Redmi Note 8 Pro,Android)完美地工作,但在我的 Pixel3a 模拟器上不起作用,它会显示一个绿色背景并带有一个 Flutter 图标。

我的启动画面为什么只在一些设备上有效?

但再次强调,在我的物理设备上它完美地工作。

英文:

So here is my desired splash screen:

我的启动画面为什么只在一些设备上有效?

And here is how I implemented it:

  1. inside android/app/src/main/res/darwable added the image of the splash screen (launch_image.png) and the gradient background xml file (gradient_background.xml)
  2. inside android/app/src/main/res/values created a file named colors.xml that's going to hold the colors to display in the splash screen:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name='gradientStartColor'>#68CDF6</color>
    <color name='gradientEndColor'>#80D325</color>
</resources>
  1. added these lines to the gradient_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="@color/gradientStartColor"
        android:endColor="@color/gradientEndColor"
        android:angle="135" />    
</shape>
  1. finally inside the launch_background.xml I added these lines:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/gradient_background"/> 

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/launch_image" />
    </item>

</layer-list>

And since there is two folders named drawable and drawable-v21, I copied the content of the first folder and put it inside the second folder because I don't really know what the secon folder is for. The splash screen is perfectly working on my physical device (Redmi Note 8 Pro, Android) but in my Pixel3a emulator it's not working, it gives me a green background with a Flutter icon in the middle.

我的启动画面为什么只在一些设备上有效?

But again, in my physical it's working perfectly.

答案1

得分: 1

在Android 12之后,启动屏幕(SplashScreen)的工作方式发生了变化,这可能是你面临的问题:https://developer.android.com/about/versions/12/features/splash-screen?hl=pt-br

默认情况下,启动屏幕会使用应用程序图标,但你可以通过一个动画图标进行更改,例如:

<item name="android:windowSplashScreenAnimatedIcon">@drawable/...</item>

并且可以通过以下方式更改颜色:

<item name="android:windowSplashScreenBackground">@color/...</item>

你可以在链接中查看其他选项。

英文:

After Android 12, has changed how SplashScreen work, so probably is the problem you are facing: https://developer.android.com/about/versions/12/features/splash-screen?hl=pt-br

By default the app icon will be used in the SplashScreen, but you can change by an animated icon, like:

&lt;item name=&quot;android:windowSplashScreenAnimatedIcon&quot;&gt;@drawable/...&lt;/item&gt;

and can change the color using:

&lt;item name=&quot;android:windowSplashScreenBackground&quot;&gt;@color/...&lt;/item&gt;

you can see another options in the link.

huangapple
  • 本文由 发表于 2023年6月29日 19:02:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定