第一步:
寻找矢量图资源
可以在阿里巴巴矢量图库寻找你心仪的的矢量图
这里以这个矢量图为例
然后选择复制SVG代码,具体代码如下:
<svg t="1656215493837" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3134" width="200" height="200"><path d="M871.1 821.4s-537.3 5.5-718.4 0c-29.4-0.9-53.2-25.1-53.2-56 0 0-5-339.5 0-519.8 0.9-30.9 23.8-56 53.2-56h718.4c29.4 0 52.3 25.1 53.2 56 4.3 143 0 519.8 0 519.8 0 30.9-23.8 56-53.2 56z" fill="#FE506B" p-id="3135"></path><path d="M382 705.1h338.9C642.2 664.3 574.8 601.4 510 546.2c-76.8 62.3-128 158.9-128 158.9z" fill="#FFBA3B" p-id="3136"></path><path d="M98.7 705c0.4 40.4 0.8 65.7 0.8 65.7 0 30.9 23.8 55.1 53.2 56 181.1 5.5 718.4 0 718.4 0 29.4 0 53.2-25.1 53.2-56 0 0 0.3-25.6 0.6-65.7H98.7z" fill="#8F3694" p-id="3137"></path><path d="M293.3 431.9c-77.5-1.6-145.5 64-195.8 132.4L98.7 705l283.3 0.1s51.2-96.6 128-158.9c-71.7-61.1-140.1-112.7-216.7-114.3zM636.6 494.7c-46.3-0.8-89.5 21.3-126.6 51.5 64.8 55.2 132.2 118.1 210.9 158.9l204.1 0.1 0.5-57.1c-101.6-68.5-187.8-151.6-288.9-153.4z" fill="#CAC134" p-id="3138"></path></svg>
第二步:
在项目的drawable文件夹下新建icon_shape.xml文件
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="1024"
android:viewportHeight="1024"
>
</vector>
这里要声明一个标签,这是在Android中使用矢量图的关键前提,这里要注意在标签内一定要给一个android:viewportWidth="“和android:viewportHeight=”"赋值这样才能正确显示矢量图。
最后在标签内编写
复制的代码中
d
对应我们的
android:pathData=
;而
fill
对应我们的
android:fillColor=
完整代码如下:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="1024"
android:viewportHeight="1024"
>
<path
android:fillColor="#FE506B"
android:pathData="M871.1 821.4s-537.3 5.5-718.4 0c-29.4-0.9-53.2-25.1-53.2-56 0 0-5-339.5 0-519.8 0.9-30.9 23.8-56 53.2-56h718.4c29.4 0 52.3 25.1 53.2 56 4.3 143 0 519.8 0 519.8 0 30.9-23.8 56-53.2 56z"/>
<path
android:fillColor="#FFBA3B"
android:pathData="M382 705.1h338.9C642.2 664.3 574.8 601.4 510 546.2c-76.8 62.3-128 158.9-128 158.9z"
/>
<path
android:fillColor="#8F3694"
android:pathData="M98.7 705c0.4 40.4 0.8 65.7 0.8 65.7 0 30.9 23.8 55.1 53.2 56 181.1 5.5 718.4 0 718.4 0 29.4 0 53.2-25.1 53.2-56 0 0 0.3-25.6 0.6-65.7H98.7z"
/>
<path
android:fillColor="#CAC134"
android:pathData="M293.3 431.9c-77.5-1.6-145.5 64-195.8 132.4L98.7 705l283.3 0.1s51.2-96.6 128-158.9c-71.7-61.1-140.1-112.7-216.7-114.3zM636.6 494.7c-46.3-0.8-89.5 21.3-126.6 51.5 64.8 55.2 132.2 118.1 210.9 158.9l204.1 0.1 0.5-57.1c-101.6-68.5-187.8-151.6-288.9-153.4z"
/>
</vector>
最终效果