編輯:關於 Android 程式設計
Android 中的 Drawable 種類繁多。它可以是一張位圖,也可以是由顏色和形狀構造出來的圖形,因此有時候它並沒有明確的「尺寸」概念。本篇會重點講解以下幾種常用的派生類:
這是最常見的一種,我們平時引用一張 png 作為背景時,系統實際上就是把它包裝成了 BitmapDrawable。它也可以通過 XML 描述,新建 bitmapdrawble.xml:
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
android:src="@drawable/ic_launcher"
android:antialias="true"
android:dither="true"
android:filter="true"
android:tileMode="mirror" />
各屬性含義如下:
把這個 bitmap 作為主布局的背景來使用:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@layout/bitmap"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sunshine.bitmapdrawble.MainActivity">
</RelativeLayout>
tileMode 設為 mirror 時的效果:

tileMode 設為 repeat 時的效果:

tileMode 設為 clamp 時的效果:

LayerDrawable 是一種分層的 Drawable,會按照 item 的聲明順序依次繪製,後繪製的圖形疊在上方。寫一個 bitmap.xml:
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item
android:left="20dp"
android:top="20dp">
<bitmap
android:gravity="center"
android:src="@drawable/image01" />
</item>
<item
android:left="20dp"
android:top="20dp">
<bitmap
android:gravity="center"
android:src="@drawable/image02" />
</item>
</layer-list>
item 上的 left/top 用來控制該層相對父容器的偏移。在主布局中定義一個 ImageView 並引用它:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sunshine.bitmapdrawble.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bitmap" />
</RelativeLayout>
效果如下圖:

可以看到兩張圖片同時呈現:image01 位於最底層,image02 疊加在它之上。
LevelListDrawable 是分級的 Drawable,核心玩法是通過 setImageLevel() 改變控件的 level 值,從而切換顯示的圖片。先建立 level.xml:
<level-list xmlns:android="https://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/image01"
android:minLevel="0"
android:maxLevel="10" />
<item
android:drawable="@drawable/image02"
android:minLevel="11"
android:maxLevel="20" />
</level-list>
規則很直白:當控件的 level 落在哪個區間,就顯示對應的那張圖。上面這份配置表示 level 在 0~10 之間顯示 image01,在 11~20 之間顯示 image02。
把它應用到主布局的 ImageView 上,並放兩個按鈕做切換:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/level" />
<Button
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClik"
android:text="第一張圖片" />
<Button
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClik"
android:text="第二張圖片" />
</LinearLayout>
Activity 中的代碼:
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv.setImageLevel(8);
}
public void onClik(View v) {
switch (v.getId()) {
case R.id.one:
iv.setImageLevel(8);
break;
case R.id.two:
iv.setImageLevel(18);
break;
default:
break;
}
}
點擊第一個按鈕顯示第一張圖,點擊第二個按鈕顯示第二張圖:


TransitionDrawable 用於實現兩個 Drawable 之間的淡入淡出(交叉淡化)效果。定義 transion.xml:
<transition xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image01" />
<item android:drawable="@drawable/image02" />
</transition>
應用到 ImageView 上:
<ImageView
android:id="@+id/iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/transion" />
Activity 中通過 startTransition() 觸發切換,參數是過渡時長(毫秒):
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
public void dochange() {
TransitionDrawable drawable = (TransitionDrawable) iv.getDrawable();
drawable.startTransition(2000);
}
public void onClik(View v) {
switch (v.getId()) {
case R.id.one:
dochange();
break;
}
}
ClipDrawable 顧名思義,是對 Drawable 做裁剪。定義 clip.xml:
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:drawable="@drawable/image01"
android:clipOrientation="horizontal"
android:gravity="left" />
其中 clipOrientation 指定裁剪方向,可取 horizontal 或 vertical;gravity 則決定從哪一側開始裁剪。
<ImageView
android:id="@+id/iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/clip" />
Activity 中通過 setLevel() 控制裁剪比例:
iv = (ImageView) findViewById(R.id.iv); ClipDrawable clip = (ClipDrawable) iv.getDrawable(); clip.setLevel(5000);
level 的取值範圍是 0~10000:0 表示完全裁剪(什麼都不顯示),10000 表示完全不裁剪,5000 則正好顯示一半。利用這個特性配合進度條,可以很輕鬆地做出自定義的進度展示效果。
以上就是這幾種 Drawable 的典型用法。
【React Native 開發】ProgressBarAndroid 進度條組件講解(12)
(一)前言 今天我們一起來看一下進度加載條 ProgressBarAndroid 控件的講解與基本使用。 該 ProgressBarAndroid 的 React 組件封
Android Activity 生命週期完整解析
Android Activity 生命週期 Activity 是 Android 應用中的單一畫面。每個 Activity 都有完整的生命週期,系統會依據使用者操作、記憶
Fragment 完全解析(上):從誕生背景到動態使用
曾經有段時間,好像聊什麼都能扯到 Fragment,做什麼都要先問一句「Fragment 能不能實現」。玩笑歸玩笑,Fragment 之所以被反覆提起,是因為它確實解決了
MVC / MVP / MVVM 三種架構模式通俗理解
上一篇我們聊了 Android 開發中常見的內存洩露場景與檢測方案。系統為每個應用分配的內存有限,一旦洩漏過多導致超出限額,便會引發內存溢出而崩潰;而日常開發中,不當的代