Flutter启动页消除白圈问题求助
Flutter Android启动页PNG图标出现白圈问题解决
我使用无背景的PNG图片作为启动图标,但启动页显示时图标周围出现白圈,相关配置代码如下:
启动页配置文件(launch_background.xml)
<?xml version="1.0" encoding="utf-8"?> <!-- Modify this file to customize your launch splash screen --> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/black" /> <!-- You can insert your own image assets here --> <item> <bitmap android:gravity="fill" android:src="@mipmap/ic_launcher" android:width="100dp" android:height="100dp" /> </item> </layer-list>
颜色配置文件(color.xml,路径:android->src->main->values)
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="black">#000000</color> </resources>
样式配置文件(style.xml,路径:android->src->main->values)
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off --> <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar"> <!-- Show a splash screen on the activity. Automatically removed when the Flutter engine draws its first frame --> <item name="android:windowBackground">@drawable/launch_background</item> </style> <!-- Theme applied to the Android Window as soon as the process has started. This theme determines the color of the Android Window while your Flutter UI initializes, as well as behind your Flutter UI while its running. This Theme is only used starting with V2 of Flutter's Android embedding. --> <style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar"> <item name="android:windowBackground">?android:colorBackground</item> </style> </resources>
问题原因及解决办法
1. 自适应图标背景导致白圈
如果项目适配Android 8.0+(API 26+),默认会使用自适应图标配置。打开res/mipmap-anydpi-v26/ic_launcher.xml,查看是否存在白色背景层:
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> <background android:drawable="@color/white"/> <!-- 此处白色背景会透过透明图标显示 --> <foreground android:drawable="@mipmap/ic_launcher_foreground"/> </adaptive-icon>
将@color/white修改为@color/black或透明色@android:color/transparent即可消除白圈。
2. 调整启动页图标布局配置
原配置中bitmap的fill重力会强制拉伸图标,固定宽高也可能导致图标变形或边缘异常。修改launch_background.xml:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/black" /> <item> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher" /> </item> </layer-list>
- 移除
android:width和android:height,保留图标原始比例 - 将
gravity改为center,让图标居中显示
3. 修改启动主题为深色主题
原LaunchTheme继承自浅色主题,可能导致窗口背景的浅色元素透过图标显示。修改style.xml中的LaunchTheme:
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar"> <item name="android:windowBackground">@drawable/launch_background</item> </style>
4. 替换为非自适应图标(可选)
如果不需要自适应图标,直接将透明背景的PNG图片替换到所有mipmap-xxx目录下的ic_launcher.png,并删除mipmap-anydpi-v26目录下的自适应图标配置文件。
内容的提问来源于stack exchange,提问作者Achraf
相关产品推荐
相关产品推荐

