Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

8.1.3 Android 13 種 Drawable 小結(三):Layer、Transition、LevelList 與 StateList

編輯:Android 技術基礎

本節引言

本節把最後四種 Drawable 收尾:LayerDrawable、TransitionDrawable、LevelListDrawable 與 StateListDrawable。一樣先附上 13 種 Drawable 的分類圖:


1. LayerDrawable

層疊圖形物件。它內部維護一個 Drawable 陣列,並依陣列順序繪製,索引值最大的會被畫在最上層。這些 Drawable 雖然會有重疊區域,但因為位於不同圖層,彼此不互相干擾。根節點為 <layer-list>。

相關屬性

  • drawable:引用的圖形資源,若為空則必須有一個 Drawable 型別的子節點。
  • left:該層相對於容器的左邊距。
  • right:右邊距。
  • top:上邊距。
  • bottom:下邊距。
  • id:該層的 id。

使用示例

程式碼實作

結合前面學過的 ShapeDrawable 與 ClipDrawable,就能客製出一條 SeekBar 的進度外觀。

layerList_one.xml

 <?xml version="1.0" encoding="utf-8"?>
 <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:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
 android:indeterminateOnly="false"
 android:maxHeight="10dp"
 android:minHeight="5dp"
 android:progressDrawable="@drawable/layerlist_one"
 android:thumb="@drawable/shape_slider" />

就這樣,沒了。既然叫「層」,當然也可以拿來做層疊圖片的效果:

層疊圖片:layerlist_two.xml

 <?xml version="1.0" encoding="utf-8"?>
 <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>

版面配置加一個 ImageView 指過去即可:

 <ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@drawable/layerlist_two" />

用 left/top 錯開三張圖,就得到便宜又好看的堆疊視覺。


2. TransitionDrawable

LayerDrawable 的子類,只能管理兩層 Drawable,並且提供透明度變化的過場動畫,讓第一層平滑淡出、第二層淡入。根節點為 <transition>,item 寫多餘的沒有作用。屬性與 LayerDrawable 類似,但要啟動動畫必須呼叫 startTransition();想反向播放則用 reverseTransition()。

使用示例

實作程式碼

在 res/drawable 建立 transition 資源檔:

 <?xml version="1.0" encoding="utf-8"?>
 <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 指向上面的資源,接著在 Activity 裡觸發:

 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);
 }
 }

如果你想要的是「多張圖片循環淡入淡出」,光靠兩個 item 是不夠的,實作方式是用 Handler 定時替換 transition 裡的兩張圖,可以參考「Android Drawable Resource 學習(七)、TransitionDrawable」的範例。


3. LevelListDrawable

用來管理一組 Drawable,為每個項目設定不同的 level 區間;繪製時會依目前的 level 值挑出對應的 Drawable 來畫。根節點為 <level-list>,本身沒有可設定的屬性,設定都落在每個 <item> 上。

item 可用屬性

  • drawable:引用的圖形資源,若為空則必須有一個 Drawable 型別的子節點。
  • minLevel:對應的 level 最小值。
  • maxLevel:對應的 level 最大值。

使用示例

程式碼實作

先用 ShapeDrawable 畫圓,複製五份只改尺寸:

shape_cir1.xml

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
 <solid android:color="#2C96ED"/>
 <size android:height="20dp" android:width="20dp"/>
 </shape>

接著切五層:

level_cir.xml

 <?xml version="1.0" encoding="utf-8"?>
 <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>

最後用定時器讓 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,就這麼簡單。


4. StateListDrawable

最後一位:StateListDrawable。名字看起來陌生,其實你早就用過了——為按鈕設定不同狀態背景的 <selector>,背後就是它。

可用屬性

  • drawable:引用的 Drawable。通常把沒有任何 state 條件的項目放最後,作為預設狀態。
  • 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

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle">
 <solid android:color="#DD788A"/>
 <corners android:radius="5dp"/>
 <padding android:top="2dp" android:bottom="2dp"/>
 </shape>

再寫 selector,selector_btn.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="https://schemas.android.com/apk/res/android">
 <item android:state_pressed="true" android:drawable="@drawable/shape_btn_pressed"/>
 <item android:drawable="@drawable/shape_btn_normal"/>
 </selector>

然後在按鈕上設 android:background="@drawable/selector_btn" 就完成了。改成矩形、橢圓或圓形都只是換個 shape 的事。

這裡有個常被忽略的細節:selector 是由上而下比對的,所以具體的 state 要寫在前面,沒有條件的預設項必須寫在最後,否則永遠不會被套用。


本節小結

13 種 Drawable 到這裡全部講完。這只是入門,實務上還有非常多組合玩法,像是把 LayerDrawable 與 LevelListDrawable 疊起來做進度條動畫,或用 StateListDrawable 配合 RippleDrawable 做 Material 風格的點擊回饋,這些就留給你自行延伸了。

時間關係,範例都是一個個試出來的,所以最後的 demo 結構比較亂,素材一併附上,需要的自行下載:DrawableDemo.zip。謝謝。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved