两步教你在安卓中快速使用矢量图

第一步:
寻找矢量图资源
可以在阿里巴巴矢量图库寻找你心仪的的矢量图
这里以这个矢量图为例
在这里插入图片描述
然后选择复制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=”"赋值这样才能正确显示矢量图。

最后在标签内编写 标签用于绘制矢量图,那么 标签内写些什么呢?相信聪明的你已经发现了,复制的SVG代码中也有 标签,不过,不需要都复制过来
复制的代码中 标签中的 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>


最终效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值