編輯:關於 Android 程式設計
Android 把「可被繪製的對象」抽象為 Drawable,不同的圖形與圖像資源對應不同的 Drawable 子類。Framework 內置的實現主要包括:ColorDrawable、GradientDrawable、BitmapDrawable、NinePatchDrawable、InsetDrawable、ClipDrawable、ScaleDrawable、RotateDrawable、AnimationDrawable、LayerDrawable、LevelListDrawable、StateListDrawable、TransitionDrawable 以及 ShapeDrawable。
日常開發中我們幾乎不會直接 new 這些對象,而是通過 XML 描述、由系統在加載資源時自動創建合適的實例——理解它們的分工,才能在寫 res/drawable 下的 XML 時知道該用哪一個。
最簡單的一種,通常在 colors.xml 中定義顏色值,設置 View 背景為純色時,實際使用的就是 ColorDrawable。可以從 View 的源碼中直接看出這層關係:
public void setBackgroundColor(@ColorInt int color) {
if (mBackground instanceof ColorDrawable) {
((ColorDrawable) mBackground.mutate()).setColor(color);
computeOpaqueFlags();
mBackgroundResource = 0;
} else {
setBackground(new ColorDrawable(color));
}
}
GradientDrawable 用於描述漸變色,XML 中通常與 <shape> 結合使用。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:startColor="#FFF"
android:endColor="#000" />
</shape>
其構造函數直接接收方向與顏色數組:
public GradientDrawable(GradientDrawable.Orientation orientation, int[] colors)
ShapeDrawable 則定義了矩形、橢圓、線、環等多種基本圖形,配合 stroke、corners、solid 等子標籤,可以覆蓋絕大多數「自定義背景」的需求,是使用頻率最高的一類。
最常見的一種。從 drawable 目錄加載的圖片,絕大多數都是 BitmapDrawable。它支持 gravity、tileMode(平鋪)等屬性,可以用來快速實現重複鋪底的效果。
點九圖(.9.png)的四週各有一像素的黑線,用來標註可拉伸區域與內容顯示區域。NinePatchDrawable 繪製的就是這樣一張可伸縮的位圖,系統會根據控件實際尺寸自動調整拉伸部分,因此特別適合製作「會被拉伸的背景」,例如對話框、按鈕底圖。
InsetDrawable 把一個 Drawable 嵌入到另一個 Drawable 內部,並在四周留出間距。它和 padding 有相似之處,但含義不同:padding 描述的是「內容與 Drawable 邊緣」的距離,而 InsetDrawable 描述的是「兩個 Drawable 與容器」之間的距離。
當你希望控件的可點擊區域大於視覺上的邊框時,InsetDrawable 就很合適——視覺上是一個小方塊,觸摸區域卻可以是整個父容器。
ClipDrawable 通過設置某個 Drawable 的當前顯示比例,裁剪出另一張 Drawable。通過 setLevel() 調節比例(取值範圍 0~10000,值越大顯示越多,10000 時完全顯示),可以實現進度條一類的效果。
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:drawable="@drawable/drawable_resource"
android:clipOrientation="horizontal"
android:gravity="left" />
clip 元素只有三個屬性:android:drawable 指定原始圖片,android:clipOrientation 指定裁剪方向(vertical 或 horizontal),android:gravity 指定從哪一側開始裁剪,可用「|」組合多個值。
顧名思義,分別用來對圖片做縮放和旋轉,同樣通過 level 控制當前比例或角度。自定義 Loading 轉圈動畫時常會用到。
把幾張圖串起來做簡單的幀動畫:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/on_001" android:duration="100" />
<item android:drawable="@drawable/on_002" android:duration="100" />
<item android:drawable="@drawable/on_003" android:duration="100" />
<item android:drawable="@drawable/on_004" android:duration="100" />
<item android:drawable="@drawable/on_005" android:duration="100" />
<item android:drawable="@drawable/on_006" android:duration="100" />
</animation-list>
LayerDrawable 管理一組 Drawable,按列表順序繪製,列表中最後一個顯示在最上層。
<layer-list>:必須的,作為根節點,包含一個或多個 <item>。
<item> 的常用屬性:
@+id/name,之後可通過 findViewById() 檢索或修改。默認情況下,layer-list 中所有 Drawable 都會被縮放以適應視圖大小,因此定義不同的位置屬性反而可能導致視圖尺寸被撐大並整體縮放。要避免縮放,可以在 item 內嵌一個 <bitmap> 並指定不會被拉伸的 gravity:
<item>
<bitmap
android:src="@drawable/image"
android:gravity="center" />
</item>
相比之下,直接寫 <item android:drawable="@drawable/image" /> 時圖片會自動縮放以適應視圖。
也就是我們熟悉的 selector。為不同狀態(pressed、focused、selected、enabled)準備不同的背景或顏色,是製作按鈕、列表項背景最常用的手段。
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/pressed" />
<item android:state_focused="true" android:drawable="@color/focused" />
<item android:drawable="@color/normal" />
</selector>
對應 <level-list> 標籤,與 selector 類似,也是一個 Drawable 集合,但它依據 Drawable 的 level 值來選擇顯示哪一項:level 落在哪個 item 的 maxLevel 區間內,就顯示那一項。
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_launcher" android:maxLevel="0" />
<item android:drawable="@drawable/icon1" android:maxLevel="1" />
</level-list>
<ImageView
android:id="@+id/cv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="@drawable/level_drawable" />
兩個坑:一是這裡必須用 src 而不是 background,否則不生效;二是切換圖片要調用 imageView.setImageLevel(1),而不是 setImageResource()。
用於實現兩個 Drawable 之間的交叉淡入淡出。每個 <item> 代表一個 Drawable,且只能有兩個。向前切換調用 startTransition(),向後切換調用 reverseTransition()。
文件位置:res/drawable/filename.xml
資源引用:Java 中為 R.drawable.filename,XML 中為 @drawable/filename。
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/on" />
<item android:drawable="@drawable/off" />
</transition>
在代碼中觸發:
ImageButton button = (ImageButton) findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable) button.getDrawable(); drawable.startTransition(500);
Android 介面排版基礎:五大版面配置解析
版面配置是介面的骨架 Android 介面由許多控制項組成,版面配置就像容器,決定控制項的排列方式。複雜畫面常會在版面配置中再嵌入版面配置,以達到巢狀排版的效果。 Lin
Android 下拉刷新庫 RefreshWidgetLib 設計解析
下拉刷新是許多 App 必備的互動模式。本文以自製的 RefreshWidgetLib 為例,說明如何透過自訂 ViewGroup、觸控事件攔截與 Header/Foot
透徹理解 Paint 的 Cap 與 Join:附圖解說
網上關於 Paint 裡 Cap 與 Join 這兩個列舉的說明大多停在表面,看過之後仍然不清楚差別在哪。這裡用圖把兩者講透。 Cap:線段端點的樣子 Cap 用來控制
Android 記憶體洩漏檢查完全指南
日前在通勤路上讀到一篇談記憶體洩漏檢查的文章,頗有收穫。記憶體洩漏的確是每個開發者都頭痛的事,這裡就順勢深入研究一下。 一、常見的垃圾回收演算法 引用計數法最簡單的策略: