Android

SVG在Android中使用

2017-03-10  本文已影响61人  刘小帅

优点:

缺点:

无脑使用

1.svg图片来源

2.SVG 转化为 VectorDrawable
http://inloop.github.io/svg2android/ 这里可以转化,不过不支持火狐浏览器,那就用Chrome浏览器呗

3.material design 插件直接能生成VectorDrawable

两种方法演示

first: 直接在阿里巴巴图标库下载一张购物车的svg格式图片

购物车.png
打开转化的网页导入图片生成下面的图片
<pre>
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="1024dp"
android:height="1024dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
    android:fillColor="#DB4E45"
    android:pathData="M235.097303 699.133018C247.384597 735.582848 286.382106 763.623932 324.772759

763.623932L861.658537 763.623932 880.429732 763.623932 885.649214 745.458146
1009.08775 315.845043C1021.388907 273.032339 993.187631 235.213675 949.051586
235.213675L424.585365 235.213675C410.791718 235.213675 399.609756 246.479262
399.609756 260.376068 399.609756 274.272875 410.791718 285.538462 424.585365
285.538462L949.051586 285.538462C959.924 285.538462 964.161052 291.220461
961.106394 301.851829L837.667859 731.464932 861.658537 713.299145 324.772759
713.299145C307.742153 713.299145 287.789837 698.952471 282.395194
682.949451L73.600164 63.566336C69.16437 50.407706 54.980454 43.363344 41.919502
47.832313 28.858551 52.301281 21.86648 66.591273 26.302275 79.749903L235.097303
699.133018Z" />
<path
android:fillColor="#DB4E45"
android:pathData="M399.609756 902.017094C399.609756 846.429871 354.881911 801.367522 299.707317
801.367522 244.532723 801.367522 199.804878 846.429871 199.804878 902.017094
199.804878 957.604318 244.532723 1002.666667 299.707317 1002.666667 354.881911
1002.666667 399.609756 957.604318 399.609756 902.017094ZM249.756098
902.017094C249.756098 874.223482 272.120021 851.692307 299.707317 851.692307
327.294613 851.692307 349.658537 874.223482 349.658537 902.017094 349.658537
929.810705 327.294613 952.341879 299.707317 952.341879 272.120021 952.341879
249.756098 929.810705 249.756098 902.017094Z" />
<path
android:fillColor="#DB4E45"
android:pathData="M924.097562 902.017094C924.097562 846.429871 879.369715 801.367522 824.195121
801.367522 769.020529 801.367522 724.292683 846.429871 724.292683 902.017094
724.292683 957.604318 769.020529 1002.666667 824.195121 1002.666667 879.369715
1002.666667 924.097562 957.604318 924.097562 902.017094ZM774.243902
902.017094C774.243902 874.223482 796.607825 851.692307 824.195121 851.692307
851.782419 851.692307 874.146342 874.223482 874.146342 902.017094 874.146342
929.810705 851.782419 952.341879 824.195121 952.341879 796.607825 952.341879
774.243902 929.810705 774.243902 902.017094Z" />
</vector>
</pre>

在drawable下新建文件黏贴这段代码,可以看到图片原本的样子

代码配图.png

直接像普通图片一样使用
<pre>
<ImageView
android:layout_width="72dp"
android:layout_height="72dp"
android:src="@drawable/test"
/>
</pre>

通过 img.setColorFilter(Color.RED);可以改变图片颜色

second: material design 插件直接生成VectorDrawable

md.png
进阶

这样使用好像太无脑了,我们总该懂点VectorDrawable里面代码的意思

仔细看pathData 的一连串数字

上一篇 下一篇

猜你喜欢

热点阅读