案例來自于開源項目,文章作為自己學(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



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動畫

在控件中使用app:srcCompat引入

使用代碼引入
