SVG在Android中使用

優(yōu)點(diǎn):

  • 文件體積小于傳統(tǒng)的位圖文件
  • 圖片是矢量圖,適應(yīng)性強(qiáng)于傳統(tǒng)的位圖
  • 再也不擔(dān)心別人解壓后盜圖了
  • 功能強(qiáng)大,不用寫很多代碼就可以實(shí)現(xiàn)非常復(fù)雜的動(dòng)畫

缺點(diǎn):

  • 渲染花費(fèi)的時(shí)間更長(zhǎng)
無(wú)腦使用

1.svg圖片來(lái)源

2.SVG 轉(zhuǎn)化為 VectorDrawable
http://inloop.github.io/svg2android/ 這里可以轉(zhuǎn)化,不過(guò)不支持火狐瀏覽器,那就用Chrome瀏覽器唄

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

兩種方法演示

first: 直接在阿里巴巴圖標(biāo)庫(kù)下載一張購(gòu)物車的svg格式圖片

購(gòu)物車.png

打開(kāi)轉(zhuǎn)化的網(wǎng)頁(yè)導(dǎo)入圖片生成下面的圖片
<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>

通過(guò) img.setColorFilter(Color.RED);可以改變圖片顏色

second: material design 插件直接生成VectorDrawable

md.png
進(jìn)階

這樣使用好像太無(wú)腦了,我們總該懂點(diǎn)VectorDrawable里面代碼的意思

  • 根節(jié)點(diǎn)vector
    1.android:width與android:height
    VectorDrawable默認(rèn)的寬高,這兩個(gè)值一定要設(shè)置
    2.android:viewportHeight與android:viewportWidth
    畫布寬高,這兩個(gè)值也必須設(shè)置

  • path標(biāo)簽
    android:fillColor 定義繪制顏色
    android:pathData 定義繪制路徑

仔細(xì)看pathData 的一連串?dāng)?shù)字

  • M 移動(dòng)繪制點(diǎn),作用相當(dāng)于把畫筆落在哪一點(diǎn)。
  • L line to 直線,就是一條直線
  • Z close 閉合
  • C cubic bezier 三次貝塞爾曲線
  • Q quatratic bezier 二次貝塞爾曲線
  • A ellipse 圓弧
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容