** VectorDrawable** android5.0開始支持矢量圖神帅,它非常適合圖標(biāo),是獨(dú)立于屏幕密度的,一個(gè)文件適合所有屏幕婚陪。當(dāng)縮放的時(shí)候還可以保存所有的圖片質(zhì)量,通常文件所占用的空間也非常小使用VectorDrawable 可以大幅度的減少apk大小频祝。最新的AppCompat23.2庫通過使用VectorDrawableCompat可以支持在android api7+版本上使用矢量圖
矢量圖svg轉(zhuǎn)vectorDrawable
android并不支持web上的svg這種矢量圖片格式,而是以VectorDrawable的方式來實(shí)現(xiàn)矢量圖的效果泌参,因此我們需要自行轉(zhuǎn)換
轉(zhuǎn)換工具
android studio自帶轉(zhuǎn)換工具(圖片上文字會(huì)丟失)
項(xiàng)目中使用
- AppCompatImageView或者AppCompatImageButton或其子類,必須在app:srcCompat標(biāo)簽中使用
- appcompat 23.2.0開始脆淹,提供了以上兩種支持庫一個(gè)用于兼容矢量圖,但是這個(gè)支持庫要使用的話,還得在app的gradle里面加個(gè)這樣的配置
//在gradle2.0及以上:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}}
* 代碼使用
<android.support.v7.widget.AppCompatImageView
android:layout_width="42dp"
android:layout_height="42dp"
app:srcCompat="@drawable/icon_shopping"/>
- TextView button上使用
- 項(xiàng)目中大量icon是用textview drawableLeft等屬性使用..
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:src="@drawable/aa_b_test" />
<TextView
android:layout_width="16dp"
android:layout_height="16dp"
android:drawableRight="@drawable/aa_b_test" />
高版本可以直接用drawable方式使用沽一,低版本需要包裹層StateListDrawable等實(shí)現(xiàn),且對(duì)應(yīng)activity加上句代碼盖溺,下個(gè)模塊會(huì)說明
兼容性解決
在Android 5.0之前(API level 21),Support Library 23.2或者更高的版本提供了矢量圖片和矢量圖片動(dòng)畫完整的支持铣缠。
- AnimatedVectorDrawableCompat通過兩個(gè)新的Support Libraries:support-vector-drawable和animated-vector-drawable分別進(jìn)行支持烘嘱;
在你app模塊的build.gradle文件中添加vectorDrawables元素,使你的app使用矢量圖support library蝗蛙;
android {
... ...
defaultConfig {
... ...
vectorDrawables.useSupportLibrary = true
}
... ...
}
普通控件上使用Vector,就必須依附于StateListDrawable,InsetDrawable,LayerDrawable,LevelListDrawable,RotateDrawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/aa_b_test"/>
</selector>
同時(shí)activity中加以下代碼
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
有個(gè)坑就是該頁面activity需繼承自AppCompatActivity以支持compat兼容包屬性
- 我們也可以采用低版本構(gòu)建生成PNG圖片的兼容方式蝇庭;
flight/build.gradle文件
apply plugin: 'com.android.library'
android {
... ...
aaptOptions {
additionalParameters "--no-version-vectors"
}
... ...
}
性能
- 運(yùn)行時(shí)間
long aaa = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.aa_b_test);
imageView.setImageDrawable(drawable);
}
Log.e("+++++++++++++++++", System.currentTimeMillis() - aaa + "++++++++++++");
結(jié)論是矢量圖4191 普通位圖3193
- size大小
矢量圖:661字節(jié),
位圖:一套圖(200*200)5kb捡硅,按項(xiàng)目要求4套大概10幾kb
總結(jié)
- 優(yōu)點(diǎn)
- 圖片擴(kuò)展性:不損傷圖片質(zhì)量哮内,一套圖適配所有
- 圖片大小:比使用位圖小十幾倍病曾,有利與減小apk size
- 缺點(diǎn)
- 性能優(yōu)損失牍蜂,系統(tǒng)渲染VectorDrawable需要花費(fèi)更多時(shí)間,因?yàn)槭噶繄D的初始化加載會(huì)比相應(yīng)的光柵圖片消耗更多的CPU周期泰涂,但是兩者之間的內(nèi)存消耗和性能接近鲫竞。
- 矢量圖主要用在色調(diào)單一的icon
可以只考慮在顯示小圖片的時(shí)候使用矢量圖(建議你限制矢量圖在200200dp)*
問題
Not supported SVG features
These SVG elements are not supported by VectorDrawable: patterns, masks, gradients, images, etc.
VectorDrawable fill-rule is always non-zero and cannot be changed prior to Android 7.0 (Nougat). If you end up with areas filled that should not be filled, that is because the SVG image was created using even-odd rule instead. There are three ways to deal with this problem: try specifying the --fix-fill-type option, manually edit SVGs in vector graphics software or convert for Android 7.0+.
所以svg轉(zhuǎn)換VectorDrawables時(shí)并不是100%還原,像我們遇到過luachicon會(huì)出現(xiàn)圓角丟失逼蒙。建議:單色調(diào)的icon使用矢量圖