英文:
Why is my splash screen work only on some devices?
问题
这是我期望的启动画面:
这是我实现的方式:
- 在
android/app/src/main/res/drawable
目录下添加了启动画面的图片 (launch_image.png
) 和渐变背景的 XML 文件 (gradient_background.xml
) - 在
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>
- 将以下行添加到
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>
- 最后,在
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>
由于存在两个名为 drawable
和 drawable-v21
的文件夹,我将第一个文件夹的内容复制到了第二个文件夹中,因为我实际上不知道第二个文件夹的用途。这个启动画面在我的物理设备上(Redmi Note 8 Pro,Android)完美地工作,但在我的 Pixel3a 模拟器上不起作用,它会显示一个绿色背景并带有一个 Flutter 图标。
但再次强调,在我的物理设备上它完美地工作。
英文:
So here is my desired splash screen:
And here is how I implemented it:
- 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
) - inside
android/app/src/main/res/values
created a file namedcolors.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>
- 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>
- 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:
<item name="android:windowSplashScreenAnimatedIcon">@drawable/...</item>
and can change the color using:
<item name="android:windowSplashScreenBackground">@color/...</item>
you can see another options in the link.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论