Android使用矢量圖

案例來自于開源項目,文章作為自己學(xué)習(xí)記錄使用
參考資料:http://blog.csdn.net/wanggang514260663/article/details/51503166
                  http://www.tuicool.com/articles/3emUnmM
                  http://blog.csdn.net/ljx19900116/article/details/41806875

一、矢量圖VectorDrawable

VectorDrawable 是android針對 svg圖片的使用提供的類,svg圖的優(yōu)點呢就是可以進行不丟失清晰度的壓縮,只需要使用一套drawable即可,再也不用提供xhdip, hdip ,這么多套圖了。

二、使用矢量圖的準(zhǔn)備

在Android Support Library 23.2中,官方加入了對Vector的向下版本兼容
VectorDrawable 矢量圖 支持 API 7 +
AnimationVerctorDrawable矢量圖動畫: 支持 API 11 +
1.項目支持23.2+的support library

compile 'com.android.support:appcompat-v7:23.2.0'

2.如果gradle的版本在2.0以下

android {
 defaultConfig { 
// 不讓gradle自動生成不同屏幕分辨率的png圖 
     generatedDensities = []
 }  
aaptOptions { 
    additionalParameters "--no-version-vectors"
 }
}

如果gradle的版本是2.0以上

android { 
defaultConfig {
 vectorDrawables.useSupportLibrary = true
 }
}

三、生成矢量圖

1.AndroidStuido提供了Vector Asset可以將svg圖片轉(zhuǎn)換成Vectordrawable

Paste_Image.png
Paste_Image.png
Paste_Image.png

2.使用開源項目轉(zhuǎn)換
http://a-student.github.io/SvgToVectorDrawableConverter.Web/
http://www.cnblogs.com/pangguoming/p/5698635.html

引入VectorDrawable

1.VectorDrawable的使用和正常的圖片沒有區(qū)別。同樣可以通過xml引入,或者代碼引入。
2.如果要使VectorDrawable添加動畫效果,需要使用animated-vector標(biāo)簽。
在res中創(chuàng)建根節(jié)點為animated-vector的xml文件
drawable:代表引入的drawable圖片
target:代表目標(biāo)動畫
name:對應(yīng)矢量圖中 path部分的名稱,表示將動畫作用于矢量圖哪一部分

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
              android:drawable="@drawable/vector_drawable">
 <target android:name="star" android:animation="@animator/star_anim" />
</animated-vector>

start_anim動畫


Paste_Image.png

在控件中使用app:srcCompat引入

Paste_Image.png

使用代碼引入

Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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