編輯:關於 Android 程式設計
在這個單元裡,我們要把剩下四種 Drawable 一次學完,它們分別是 LayerDrawable、TransitionDrawable、LevelListDrawable 與 StateListDrawable。先貼上 13 種 Drawable 的導覽圖,方便你對整體有個概念:
LayerDrawable 內部維護一個 Drawable 陣列,並依照陣列順序把它們層層堆疊繪製出來,索引值越大的會被畫在最上層。這些 Drawable 即使有重疊區域也不會互相干擾,因為它們各自處於不同的圖層,根節點使用 <layer-list>。
常見屬性如下:
drawable:引用的點陣圖資源,若為空則需有一個 Drawable 類型的子節點;left / right / top / bottom:圖層相對於容器的四個邊距;id:該圖層的識別碼。
使用示例:

結合前面學過的 shapeDrawable 與 ClipDrawable,實作非常簡單:
layerList_one.xml
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<solid android:color="#C2C2C1" />
<corners android:radius="50dp" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape android:shape="rectangle">
<solid android:color="#BCDA73" />
<corners android:radius="50dp" />
</shape>
</clip>
</item>
</layer-list>
接著在佈局中放一個 SeekBar:
<seekbar
android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
android:indeterminateOnly="false"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxHeight="10dp"
android:minHeight="5dp"
android:progressDrawable="@drawable/layerlist_one"
android:thumb="@drawable/shape_slider" />
沒了?對,就是這麼點東西。既然它是層級圖形,我們還能玩出「層疊圖片」的效果:

layerlist_two.xml:
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item>
<bitmap android:gravity="center" android:src="@mipmap/ic_bg_ciwei" />
</item>
<item android:left="25dp" android:top="25dp">
<bitmap android:gravity="center" android:src="@mipmap/ic_bg_ciwei" />
</item>
<item android:left="50dp" android:top="50dp">
<bitmap android:gravity="center" android:src="@mipmap/ic_bg_ciwei" />
</item>
</layer-list>
再在 activity_main.xml 裡擺一個 ImageView 引用它即可:
<imageview
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/layerlist_two" />

簡單好用,趕快把它應用到你的專案裡吧!
它是 LayerDrawable 的子類別,只管理兩層 Drawable——注意,就是兩層!它提供了透明度漸變的動畫,能讓一層 Drawable 平滑過渡到另一層。根節點為 <transition>,一樣只放兩個 item,屬性與 LayerDrawable 相近。要啟動切換動畫需呼叫 startTransition 方法,若想反向播放則可呼叫 reverseTransition()。
使用示例:

在 res/drawable 下建立 TransitionDrawable 的 XML:
<transition xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_bg_meizi1" />
<item android:drawable="@mipmap/ic_bg_meizi2" />
</transition>
佈局裡放一個 ImageView 並把 src 指向這個 drawable,MainActivity 內容如下:
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
TransitionDrawable td = (TransitionDrawable) img_show.getDrawable();
td.startTransition(3000);
// 若想反向播放,使用 reverseTransition 即可
// td.reverseTransition(3000);
}
}
如果你想實現多張圖片循環淡入淡出的效果,核心原理其實很簡單:用 Handler 定時替換 Transition 中的兩張圖片即可。
它用來管理一組 Drawable,並為每個 Drawable 設定不同的 level。繪製時會依據目前的 level 值挑選對應的 Drawable 呈現到畫布上,根節點為 <level-list>。它本身沒有可設定的屬性,我們能調整的只有每個 <item> 的屬性。
item 可設定的屬性:
drawable:引用的點陣圖資源,若為空則需有一個 Drawable 類型的子節點;minLevel:對應的 level 最小值;maxLevel:對應的 level 最大值。
使用示例:

先用 shapeDrawable 畫圓,一式五份,只改寬高:
shape_cir1.xml:
<shape android:shape="oval" xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#2C96ED" />
<size android:height="20dp" android:width="20dp" />
</shape>
接著建立 LevelListDrawable,這裡設定五層:
level_cir.xml:
<level-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_cir1" android:maxLevel="2000" />
<item android:drawable="@drawable/shape_cir2" android:maxLevel="4000" />
<item android:drawable="@drawable/shape_cir3" android:maxLevel="6000" />
<item android:drawable="@drawable/shape_cir4" android:maxLevel="8000" />
<item android:drawable="@drawable/shape_cir5" android:maxLevel="10000" />
</level-list>
最後在 MainActivity 裡用定時器動態切換 level:
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
private LevelListDrawable ld;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
if (ld.getLevel() > 10000) ld.setLevel(0);
img_show.setImageLevel(ld.getLevel() + 2000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
ld = (LevelListDrawable) img_show.getDrawable();
img_show.setImageLevel(0);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
}
}, 0, 100);
}
}
原理很直白:一個 Timer 定時器搭配 Handler 不斷修改 level 值。
終於來到最後一種 Drawable。這個名字乍看陌生,其實我們早就用過了——還記得為按鈕設定不同狀態樣式的 <selector> 嗎?背後靠的就是它!
可設定的屬性:
drawable:引用的 Drawable,放在最前面即代表元件的正常狀態;state_focused:是否取得焦點;state_window_focused:是否取得視窗焦點;state_enabled:元件是否可用;state_checkable:元件是否可勾選(如 checkbox);state_checked:元件是否已勾選;state_selected:元件是否被選取;state_pressed:元件是否被按下;state_active:元件是否處於活動狀態;state_single / state_first / state_middle / state_last:元件含多個子元件時,用來控制各子元件的顯示狀態。
使用示例:來做一個簡單的圓角按鈕吧!

先用 shapeDrawable 畫兩個顏色不同的圓角矩形:
shape_btn_normal.xml:
<shape android:shape="rectangle" xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#DD788A" />
<corners android:radius="5dp" />
<padding android:bottom="2dp" android:top="2dp" />
</shape>
接著寫一個 selector:selctor_btn.xml:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_btn_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/shape_btn_normal" />
</selector>
最後把按鈕的 android:background 設為 "@drawable/selctor_btn" 就完成了。你也可以依需求把它改成方形、橢圓或圓形,靈活度很高。
Android 網路連線處理方式整理
在 Android 中,常見的網路連線方式包括使用 URLConnection 或 HttpURLConnection、使用 HttpClient,以及透過 WebVie
Android EditText 為什麼只剩下面一條橫線?改回傳統樣式的方法
從 Android 4.0 開始,EditText 的預設外觀被換成了 Material 之前的「只有底線」風格——輸入框本身完全透明,只在下方留一條細線。做的時候覺得很
Android Studio 教程(三):常用快捷鍵
今天是個大日子,Android Studio 1.0 終於發布了正式版,對於 Android 開發者來說無疑是喜聞樂見的消息,那就果斷下載來用吧。 官方下載地址:htt
安卓手機防中毒指南 四招遠離惡意流氓軟件
智能手機越來越深入生活,伴隨而來的隱患也層出不窮。其中讓人最頭疼的,莫過於惡意流氓軟件與手機病毒。究竟該如何守住自己的手機?小編整理了四個實用的自保技巧。 一、挑一個靠