Android: 如何在 mipmaps 图标启动器中添加阴影

4

你好,有人可以向我展示如何在Android中为应用程序图标添加阴影吗? 我有以下mipmap启动器图标:

anydpi.xml

<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@color/ic_launcher_background"/>
    <foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

ic_launcher_background.xml

<resources>
    <color name="ic_launcher_background">#448AFF</color>
</resources>

ic_launcher_foreground.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="1347.3684"
android:viewportHeight="1347.3684">


<group android:translateX="417.6842"
      android:translateY="417.6842">
      <path android:fillColor="#424A60" android:pathData="M445.793,512H128V0h317.793c9.754,0 17.655,7.901 17.655,17.655v476.69C463.448,504.099 455.548,512 445.793,512"/>
      <path android:fillColor="#586789" android:pathData="M128,512H92.69c-9.754,0 -17.655,-7.901 -17.655,-17.655V17.655C75.034,7.901 82.935,0 92.69,0H128V512z"/>
      <path android:fillColor="#CCD0D2" android:pathData="M92.69,52.966h-35.31c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828S97.562,52.966 92.69,52.966"/>
      <path android:fillColor="#CCD0D2" android:pathData="M92.69,123.586h-35.31c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828S97.562,123.586 92.69,123.586"/>
      <path android:fillColor="#CCD0D2" android:pathData="M92.69,194.207h-35.31c-4.873,0 -8.828,-3.946 -8.828,-8.828c0,-4.882 3.955,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828C101.517,190.261 97.562,194.207 92.69,194.207"/>
      <path android:fillColor="#CCD0D2" android:pathData="M92.69,264.828h-35.31c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828S97.562,264.828 92.69,264.828"/>
      <path android:fillColor="#CCD0D2" android:pathData="M92.69,335.448h-35.31c-4.873,0 -8.828,-3.946 -8.828,-8.828c0,-4.882 3.955,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828C101.517,331.502 97.562,335.448 92.69,335.448"/>
      <path android:fillColor="#CCD0D2" android:pathData="M92.69,406.069h-35.31c-4.873,0 -8.828,-3.946 -8.828,-8.828c0,-4.882 3.955,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828C101.517,402.123 97.562,406.069 92.69,406.069"/>
      <path android:fillColor="#CCD0D2" android:pathData="M92.69,476.69h-35.31c-4.873,0 -8.828,-3.946 -8.828,-8.828c0,-4.882 3.955,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828C101.517,472.744 97.562,476.69 92.69,476.69"/>
      <path android:fillColor="#25AE88" android:pathData="M331.034,406.069c0,-29.255 -23.711,-52.966 -52.966,-52.966s-52.966,23.711 -52.966,52.966s23.711,52.966 52.966,52.966S331.034,435.324 331.034,406.069"/>
      <path android:fillColor="#CC4B4C" android:pathData="M348.69,344.276v35.31h-24.797c4.511,7.795 7.142,16.825 7.142,26.483c0,7.274 -1.474,14.204 -4.122,20.515c6.444,3.69 13.815,5.967 21.778,5.967c24.373,0 44.138,-19.765 44.138,-44.138S373.063,344.276 348.69,344.276"/>
      <path android:fillColor="#F0CE49" android:pathData="M260.414,291.31v23.499l19.897,7.239l-11.626,31.938c3.054,-0.547 6.179,-0.883 9.384,-0.883c19.597,0 36.661,10.664 45.824,26.483h24.797V291.31H260.414z"/>
      <path android:fillColor="#CC4B4C" android:pathData="M268.687,353.983l11.626,-31.929l-66.366,-24.152l-24.152,66.357l42.39,15.431C239.838,366.412 253.062,356.781 268.687,353.983"/>
      <path android:fillColor="#586789" android:pathData="M278.069,97.103h-88.276c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h88.276c4.873,0 8.828,3.946 8.828,8.828S282.942,97.103 278.069,97.103"/>
      <path android:fillColor="#586789" android:pathData="M401.655,158.897h-61.793c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h61.793c4.873,0 8.828,3.946 8.828,8.828S406.528,158.897 401.655,158.897"/>
      <path android:fillColor="#586789" android:pathData="M286.901,158.897h-61.793c-4.882,0 -8.828,-3.946 -8.828,-8.828s3.946,-8.828 8.828,-8.828h61.793c4.873,0 8.828,3.946 8.828,8.828S291.774,158.897 286.901,158.897"/>
      <path android:fillColor="#586789" android:pathData="M189.793,158.897h-17.655c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h17.655c4.873,0 8.828,3.946 8.828,8.828S194.666,158.897 189.793,158.897"/>
      <path android:fillColor="#586789" android:pathData="M401.655,194.207H384c-4.873,0 -8.828,-3.946 -8.828,-8.828c0,-4.882 3.955,-8.828 8.828,-8.828h17.655c4.873,0 8.828,3.946 8.828,8.828C410.483,190.261 406.528,194.207 401.655,194.207"/>
      <path android:fillColor="#586789" android:pathData="M348.69,194.207h-88.276c-4.873,0 -8.828,-3.946 -8.828,-8.828c0,-4.882 3.955,-8.828 8.828,-8.828h88.276c4.873,0 8.828,3.946 8.828,8.828C357.517,190.261 353.562,194.207 348.69,194.207"/>
      <path android:fillColor="#586789" android:pathData="M207.453,194.207h-35.31c-4.882,0 -8.828,-3.946 -8.828,-8.828c0,-4.882 3.946,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828C216.28,190.261 212.326,194.207 207.453,194.207"/>
      <path android:fillColor="#586789" android:pathData="M401.655,229.517h-88.276c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h88.276c4.873,0 8.828,3.946 8.828,8.828S406.528,229.517 401.655,229.517"/>
      <path android:fillColor="#586789" android:pathData="M260.414,229.517h-35.31c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h35.31c4.873,0 8.828,3.946 8.828,8.828S265.287,229.517 260.414,229.517"/>
      <path android:fillColor="#586789" android:pathData="M189.793,229.517h-17.655c-4.873,0 -8.828,-3.946 -8.828,-8.828s3.955,-8.828 8.828,-8.828h17.655c4.873,0 8.828,3.946 8.828,8.828S194.666,229.517 189.793,229.517"/>
  </group>
</vector>

这段代码的输出结果如下所示:

Actual Output

然而,我想要它有一个像这样的阴影效果:

Desired Output

请不要建议我使用此链接:Android Asset Launcher Icon,因为我已经尝试过了,并且我不喜欢它,因为它将图标转换为图像,而不是矢量可绘制对象,并且它不支持自适应图标。请只提供矢量可绘制对象的答案。
请帮帮我。
1个回答

1
也许这会帮助其他人。我曾遇到同样的问题,但我没有找到任何自动化处理的工具,所以最终我按照Inkscape软件的教程手动创建了阴影。另一个我听说过的替代Inkscape的工具是Adobe Illustrator。
在创建SVG图标后,我遇到了一些问题:
  • Android Studio Asset Studio导入SVG图标时没有阴影。它显示错误“渐变没有停止信息”。 我在StackOverflow上找到了一篇文章,分享了一个工具,可以修改SVG图标使其与Android Studio兼容,并解决了这个问题。
  • 我的一台测试设备在尝试显示SVG时崩溃了。Logcat显示以下错误: java.lang.IllegalArgumentException: R is not a valid verb. Failure occurred at position 2 of path: STRING_TOO_LARGE。问题在于我的阴影的pathData太大了。我使用Inkscape中的“简化”功能缩短了阴影的路径,保持整体形状基本相同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接