編輯:關於 Android 程式設計
SVG 是一種用於描述圖像的標記語言,風格上與 HTML 頗為相似。它嚴格遵循 XML 語法,以純文字的描述性語言來定義圖像內容,因此是一種與解析度無關的向量圖形格式。
W3C 目前仍在研究制定 SVG2。
下面是一個紅色三角形的 SVG 範例:
效果圖如下:
說明:
svg 向量圖的根節點就是 svg,包含圖片寬高與視口大小等資訊。rect 表示一個矩形;path 表示一條路徑——向量圖的形狀正是透過 path 元素定義的,常見屬性有:
M (x y)+:在給定的 (x, y) 座標開始 path;作為向量圖,SVG 縮放時不會失真,而點陣圖放大後往往會出現鋸齒與模糊。此外,它讓多螢幕適配變得輕鬆:過去我們得在 ldpi、mdpi、hdpi、xhdpi 等目錄各放一份對應解析度的 jpg/png,如今只需一個 xml 檔即可,而且 xml 的體積遠小於點陣圖,有助於精簡 APK 大小。
在 Android 中,負責向量圖加載、解析與繪製的核心類是 VectorDrawable。它透過加載一份基於 XML 的 VectorDrawable 檔,在 draw 方法中把向量圖繪製到 Canvas 上。實務中,向量圖通常用於圖示與按鈕。
1. 先用 Method Draw 編輯一個 svg 格式的向量圖,完成後導出 svg 檔。
heart.svg
2. 把 svg 檔轉換為 Android 的 VectorDrawable 檔。
手動轉換
1. 在 drawable 目錄下新建一個根節點為 的 xml 檔。
2. 把 svg 中的 path 數據複製到 裡。例如 svg 中 path 的 d 對應 中 path 的 android:pathData,stroke 對應 android:strokeColor,stroke-width 對應 android:strokeWidth 等等。
使用工具轉換
用 svg2android 把 svg 檔轉換為 Android Drawable 檔,再把導出的檔放進 drawable 目錄。
res/drawable/vector_drawable_heart.xml
android:width="580dp" android:height="400dp" android:viewportWidth="580" android:viewportHeight="400"> 注意:轉換工具會默認把「畫布尺寸」當成向量圖的實際寬高。比如一個 icon 設計時畫布是 480*480,但實際只需要 48*48,這時記得把 中的 android:width 和 android:height 改成實際需要的尺寸,否則會白白浪費記憶體。
3. 使用向量圖的範例

目前這種寫法只支援 Android 5.0 及以上版本。若想相容 5.0 以下的系統,還需要做一些向下相容的處理。
1. 在 app 的 build.gradle 中加入以下配置,啟用 support 庫:
android { defaultConfig { vectorDrawables.useSupportLibrary = true } } dependencies { compile 'com.android.support:appcompat-v7:23.3.0' } 2. 把 ImageView 換成 support 庫的 AppCompatImageView,並用 app:srcCompat 取代 android:src。
AnimatedVectorDrawable 能讓 VectorDrawable 擁有動畫效果。一般需要三個檔:
VectorDrawable 檔:根節點為 的 xml,即向量圖本身。 的 xml,用於定義向量圖動畫。1. VectorDrawable 檔如下。透過 android:name 屬性為 group 和 path 定義了唯一名稱,AnimatedVectorDrawable 檔正是靠這個名稱來關聯 group/path 與對應動畫。
效果圖如下:

2. 屬性動畫檔的編寫。下面是 CPU 頂部排線的動畫檔,底部與左右排線還有類似的動畫檔,一共四個。
3. AnimatedVectorDrawable 檔如下。根節點為 ,透過 android:drawable 指定使用的向量圖檔; 中的 android:name 對應 VectorDrawable 裡 group/path 的名稱,android:animation 則指定該 group/path 使用的動畫。
4. 最後,在 AppCompatImageView 中使用 AnimatedVectorDrawable。
在 xml 中使用: 在程式碼中使用:透過 AnimatedVectorDrawableCompat.create 方法加載 AnimatedVectorDrawable 檔,得到 AnimatedVectorDrawable 對象。
AnimatedVectorDrawableCompat 是 support 庫中的類,作用是讓 AnimatedVectorDrawable 相容 Android 5.0 以下系統。
image1 = (AppCompatImageView) findViewById(R.id.image1); AnimatedVectorDrawableCompat animatedVectorDrawable = AnimatedVectorDrawableCompat.create(this, R.drawable.animated_cpu); image1.setImageDrawable(animatedVectorDrawable); addClickAnimationListener(image1);點擊以停止/播放動畫:
public void addClickAnimationListener(final AppCompatImageView image) { image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Drawable animation = image.getDrawable(); if (animation instanceof Animatable) { Animatable anim = (Animatable) animation; if (anim.isRunning()) { // 停止動畫 anim.stop(); } else { // 播放動畫 anim.start(); } } } }); }動畫效果如下:

注意:目前 AnimatedVectorDrawableCompat 在 Android 5.0 以下尚不支援 pathData 類型的屬性動畫。如果需要用到 pathData 屬性動畫,可以透過以下方式相容:
使用 api 標識符適配不同版本,在 drawable 和 drawable-v21 目錄下定義不同的實現。 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { // 在android5.0+下執行下面代碼 }使用 vector-compat——一個同時相容 VectorDrawable 與 AnimatedVectorDrawable 的第三方庫。我給 VectorDrawable 的 path 添加了一個透明度動畫,結果發現毫無反應,日誌打印如下:
W/PropertyValuesHolder: Method setAlpha() with type int not found on target class class android.support.graphics.drawable.VectorDrawableCompat$VGroup W/PropertyValuesHolder: Method setRotation() with type float not found on target class class android.support.graphics.drawable.VectorDrawableCompat$VFullPath從日誌可以看出,系統根本找不到對應的 set 方法。屬性動畫的實現原理,其實是透過 Java 反射找到相關的 set 方法,再動態修改屬性值。
VectorDrawable(這裡用的是相容 5.0 以下的 VectorDrawableCompat)中,group 對應的類是 VectorDrawableCompat$VGroup,path 對應的類是 VectorDrawableCompat$VFullPath。
查看 VectorDrawableCompat$VFullPath 的程式碼後發現,VFullPath 裡透明度屬性的名稱是 fillAlpha。於是把 android:propertyName 的值從 alpha 改成 fillAlpha,問題就解決了。
範例程式碼工程地址:https://coding.net/u/linchaolong/p/AndroidSvgExample/git
VectorDrawable 與 AnimatedVectorDrawable 的一個相容庫,目前支援 API 14+。
GitHub 地址:https://github.com/wnafee/vector-compat
AndroidSVG 是 Android 平台下的 SVG 解析與渲染庫,目前幾乎完整支援 SVG 1.1 的靜態可視元素,以及 SVG 1.2 的小部分規範(filters 除外),支援 API 8+。
支援 svg 向量圖的 ImageView:
GitHub 地址:https://github.com/BigBadaboom/androidsvg 官網:https://bigbadaboom.github.io/androidsvg/
android-pathview 是一個路徑動畫庫,路徑可來自 svg 或標準 Paths,能讓 svg 或 path 具備動畫效果,並可自由改變路徑的顏色與寬度。
GitHub 地址:https://github.com/geftimov/android-pathview
Android 4.4 以後,AnimatedVectorDrawable 能讓兩個 SVG 圖像無縫過渡(即變形動畫),但前提是這兩個 svg 的 path 參數個數必須相等、類型也要匹配(也就是格式要對齊),否則會拋出異常。簡單的 path 可以手動對齊,複雜的就很難搞定了。這個工具能透過命令列把任意兩個 svg 資源轉換成對齊模式,且不改變原圖外觀。
GitHub 地址:https://github.com/bonnyfone/vectalign
Android 5.0 Linux 內核編譯流程
環境 本文以 Windows 7 64 位元、Ubuntu 12.04 64 位元與 VMware 虛擬機為例,說明 Android 5.0 與 goldfish 內核的
EventBus 3.0.0 使用總結:依賴引入、線程模型與黏性事件
前言:為什麼選 greenrobot 的 EventBus 「EventBus」其實是個通用叫法。Google 的 Guava 也附帶了一個 EventBus 功能,但
Android 資料儲存三種方式入門:SharedPreferences、檔案與 SQLite
Android 上的本機資料儲存,實務上最常打交道的就是三種方式:SharedPreferences、檔案與 SQLite。三者定位不同:SharedPreference
SweetAlertDialog 在 Android Studio 的導入與常見錯誤
SweetAlertDialog 是什麼?SweetAlertDialog 是一個常見的 Android 對話框庫,提供比系統 AlertDialog 更鮮明的視覺效果,