** VectorDrawable** android5.0開始支持矢量圖,它非常適合圖標,是獨立于屏幕密度的,一個文件適合所有屏幕。當縮放的時候還可以保存所有的圖片質(zhì)量,通常文件所占用的空間也非常小使用VectorDrawable 可以大幅度的減少apk大小。最新的AppCompat23.2庫通過使用VectorDrawableCompat可以支持在android api7+版本上使用矢量圖
矢量圖svg轉(zhuǎn)vectorDrawable
android并不支持web上的svg這種矢量圖片格式,而是以VectorDrawable的方式來實現(xiàn)矢量圖的效果,因此我們需要自行轉(zhuǎn)換
轉(zhuǎn)換工具
android studio自帶轉(zhuǎn)換工具(圖片上文字會丟失)
項目中使用
- AppCompatImageView或者AppCompatImageButton或其子類,必須在app:srcCompat標簽中使用
- appcompat 23.2.0開始,提供了以上兩種支持庫一個用于兼容矢量圖,但是這個支持庫要使用的話,還得在app的gradle里面加個這樣的配置
//在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上使用
- 項目中大量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等實現(xiàn),且對應activity加上句代碼,下個模塊會說明
兼容性解決
在Android 5.0之前(API level 21),Support Library 23.2或者更高的版本提供了矢量圖片和矢量圖片動畫完整的支持。
- AnimatedVectorDrawableCompat通過兩個新的Support Libraries:support-vector-drawable和animated-vector-drawable分別進行支持;
在你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>
同時activity中加以下代碼
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
有個坑就是該頁面activity需繼承自AppCompatActivity以支持compat兼容包屬性
- 我們也可以采用低版本構(gòu)建生成PNG圖片的兼容方式;
flight/build.gradle文件
apply plugin: 'com.android.library'
android {
... ...
aaptOptions {
additionalParameters "--no-version-vectors"
}
... ...
}
性能
- 運行時間
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,按項目要求4套大概10幾kb
總結(jié)
- 優(yōu)點
- 圖片擴展性:不損傷圖片質(zhì)量,一套圖適配所有
- 圖片大?。罕仁褂梦粓D小十幾倍,有利與減小apk size
- 缺點
- 性能優(yōu)損失,系統(tǒng)渲染VectorDrawable需要花費更多時間,因為矢量圖的初始化加載會比相應的光柵圖片消耗更多的CPU周期,但是兩者之間的內(nèi)存消耗和性能接近。
- 矢量圖主要用在色調(diào)單一的icon
可以只考慮在顯示小圖片的時候使用矢量圖(建議你限制矢量圖在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時并不是100%還原,像我們遇到過luachicon會出現(xiàn)圓角丟失。建議:單色調(diào)的icon使用矢量圖