編輯:Android 技術基礎
本節繼續往下學。上一節我們完成了四種:
ColorDrawable、
NinePatchDrawable、
ShapeDrawable、
GradientDrawable。
這次一口氣處理五個:
BitmapDrawable、
InsetDrawable、
ClipDrawable、
RotateDrawable、
AnimationDrawable。
一樣先附上 13 種 Drawable 的分類圖:
對 Bitmap 的一層封裝,可以決定它該怎麼填進自己的邊界:填滿、拉伸填滿,或保持原始尺寸。根節點為 <bitmap>。可選屬性如下:
- src:圖片資源。
- antialias:是否啟用抗鋸齒。
- filter:是否啟用位圖濾波,開啟後縮放顯示較平滑。
- dither:是否對位圖做色像抖動處理。
- gravity:當位圖比容器小時,決定它在容器中的位置。
- tileMode:平鋪填滿的模式。設了這個屬性後 gravity 會被忽略。可選值:disabled(拉伸鋪滿)、clamp(沿用原圖尺寸)、repeat(規則平鋪)、mirror(鏡像平鋪)。
對應效果圖:
① 用 XML 定義
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="https://schemas.android.com/apk/res/android" android:dither="true" android:src="@drawable/ic_launcher" android:tileMode="mirror" />
② 等效的 Java 寫法
BitmapDrawable bitDrawable = new BitmapDrawable(bitmap); bitDrawable.setDither(true); bitDrawable.setTileModeXY(TileMode.MIRROR, TileMode.MIRROR);
把一個 Drawable 內縮嵌入到容器中,並在四周留出間距。它和 padding 不一樣:padding 描述的是 Drawable 的內容與自身邊界的距離,而 InsetDrawable 描述的是 Drawable 本身與外層容器的距離。
因此當你「需要的背景比實際邊框小一圈」時,InsetDrawable 就很合用。最典型的場景是自訂 Dialog 與螢幕之間的間距——這種情況下設 layout_margin 是沒有效果的,改用 InsetDrawable 當背景才是正解。
相關屬性
- drawable:引用的 Drawable,若為空則必須有一個 Drawable 型別的子節點。
- visible:Drawable 是否可見。
- insetLeft、insetRight、insetTop、insetBottom:四個方向的內縮距離。
① XML 寫法
<?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="https://schemas.android.com/apk/res/android" android:drawable="@drawable/test1" android:insetBottom="10dp" android:insetLeft="10dp" android:insetRight="10dp" android:insetTop="10dp" />
② Java 寫法
InsetDrawable insetDrawable = new InsetDrawable(getResources().getDrawable(R.drawable.test1), 10, 10, 10, 10);
執行結果

Clip 有「剪」的意思,ClipDrawable 可以理解成從 Drawable 上剪下一塊來顯示。Android 的 ProgressBar 就是靠它實作的:依 level 值決定要顯示的比例。根節點為 <clip>。
相關屬性
- clipOrientation:裁剪方向,可設水平或垂直。
- gravity:從哪個位置開始裁。
- drawable:引用的 Drawable,為空時需要一個 Drawable 型別的子節點,寫法是在 <clip> 裡再包一層 <bitmap android:src="@drawable/test4" android:gravity="center" />。
使用示例
關鍵在於透過程式去改 ClipDrawable 的 level,範圍是 0 到 10000。

① 定義 clip 資源
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="https://schemas.android.com/apk/res/android" android:clipOrientation="horizontal" android:drawable="@mipmap/ic_bg_meizi" android:gravity="left" />
② 版面配置放一個 ImageView,src 指向該資源
這裡務必注意:要用 src,寫成 background 會拿到 null 而丟出空指標例外。
<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/img_show" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/clip_bg" /> </LinearLayout>
③ 用 setLevel 控制顯示比例
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
private ClipDrawable cd;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
cd.setLevel(cd.getLevel() + 500);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
// 核心:取得 ClipDrawable 並定時推進 level
cd = (ClipDrawable) img_show.getDrawable();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
if (cd.getLevel() >= 10000) {
timer.cancel();
}
}
}, 0, 300);
}
}
順便提醒:在非 UI 執行緒直接改 Drawable 是不安全的,正式專案請改用 Handler 或 runOnUiThread 統一在主執行緒操作。
用來旋轉 Drawable,同樣透過 setLevel 控制,最大值也是 10000。
相關屬性
- fromDegrees:起始角度,對應最低 level,預設 0。
- toDegrees:結束角度,對應最高 level,預設 360。
- pivotX:旋轉中心的 x 座標,取值 0 到 1,預設 50%(即 0.5)。
- pivotY:旋轉中心的 y 座標,取值 0 到 1,預設 50%。若遇到旋轉後圖片顯示不全,調這兩個值通常就能解決。
- drawable:要旋轉的圖形資源。
- visible:Drawable 是否可見。
角度示意

使用示例

程式碼實作:在上一點的基礎上小改即可。
① 定義 rotate 資源
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="https://schemas.android.com/apk/res/android" android:drawable="@mipmap/ic_launcher" android:fromDegrees="-180" android:pivotX="50%" android:pivotY="50%" />
② 版面配置的 src 指向上面的資源,Activity 只要把 ClipDrawable 換成 RotateDrawable
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
private RotateDrawable cd;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
if (cd.getLevel() >= 10000) {
Toast.makeText(MainActivity.this, "轉完了~", Toast.LENGTH_LONG).show();
}
cd.setLevel(cd.getLevel() + 400);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
cd = (RotateDrawable) img_show.getDrawable();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
if (cd.getLevel() >= 10000) {
timer.cancel();
}
}
}, 0, 100);
}
}
本節最後一位,用來實作 Android 的frame 動畫:把一系列 Drawable 依序一張張播放。Android 的動畫體系很完整(補間、alpha、scale、translate、rotate 等),但那些留到動畫章節再講,這裡只談 frame 動畫。
根節點用 <animation-list>。
相關屬性與方法
oneshot:是否只播放一次;設為 false 才會循環播放。
duration:每一幀的停留時間,常見設定為 100~300 毫秒。
取得 AnimationDrawable 物件後要呼叫 start() 才會開始播放。這裡有個很常踩的坑:在 onCreate() 裡直接呼叫是不會有效果的,因為 View 還沒完成初始化。解法是用 Handler 的 postDelayed 延一點再啟動。
使用示例

① 定義 animation-list 資源
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="https://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@mipmap/ic_pull_to_refresh_loading01" android:duration="100" /> <item android:drawable="@mipmap/ic_pull_to_refresh_loading02" android:duration="100" /> <item android:drawable="@mipmap/ic_pull_to_refresh_loading03" android:duration="100" /> <item android:drawable="@mipmap/ic_pull_to_refresh_loading04" android:duration="100" /> <item android:drawable="@mipmap/ic_pull_to_refresh_loading05" android:duration="100" /> <item android:drawable="@mipmap/ic_pull_to_refresh_loading06" android:duration="100" /> </animation-list>
② 版面配置設好 src,Activity 延遲啟動動畫
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
private AnimationDrawable ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
// 核心:延遲啟動,確保 View 已完成初始化
ad = (AnimationDrawable) img_show.getDrawable();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
ad.start();
}
}, 300);
}
}
用 AnimationDrawable 做 frame 動畫真的非常方便,適合那種「不需要中途控制」的場合,例如下拉刷新的 loading 轉圈動畫。如果需要暫停、跳幀或跟手勢互動,就得換 ObjectAnimator 或自訂 ValueAnimator 了。
本節又介紹了五個 Drawable:BitmapDrawable 負責平鋪與填充,InsetDrawable 解決內縮間距,ClipDrawable 與 RotateDrawable 靠 level 做進度與旋轉,AnimationDrawable 則負責 frame 動畫。這幾個組合起來能做出不少東西,值得實際用進專案試試。
另外回應一下有讀者關心舊文會不會被刪:只會刪掉與新內容重複的那幾篇,而且刪除前都會備份,不用擔心。
2.6.1 PopupWindow 懸浮視窗基本用法
前言 本節介紹的是最後一個用於顯示資訊的 UI 元件——PopupWindow(懸浮視窗)。想知道它長什麼樣?打開手機裡的 QQ,長按清單上的某一項,這時會彈出一個黑色
第69章、初識 Intent — 開啟另一個 Activity 並單向傳值(從零開始學 Android)
在 Android 應用中,Activity 之間的跳轉是透過 Intent 機制完成的。本案例將簡單介紹如何運用 Intent,讓程式由 MainActivity 跳轉
Android AsyncTask 異步任務入門
為什麼需要異步任務 Android 的 UI 更新必須在主執行緒中進行。若把耗時工作放在主執行緒,可能造成畫面卡頓,甚至引發 ANR。因此,網路請求、檔案讀寫、圖片處理等
Paint API 之 Typeface(字型)設定
本節帶來 Paint API 系列的最後一個 API——Typeface(字型)。顧名思義,這個 API 用來設定字體以及字體風格,使用起來也非常簡單。下面我們來學習 T