編輯:關於 Android 開發
第六章 Android 的 Drawable
Drawable 最大的優點在於使用簡單、成本低於自訂 View;而且非圖片類型的 Drawable 佔用空間極小,有助於縮減 APK 的體積。
6.1 Drawable 簡介
Drawable 家族成員眾多,它們都代表著某種「圖像」的概念,最常被用來作為 View 的背景。Drawable 本身是一個抽象類別,其中「固有寬高」是相當重要的參數,可透過 getIntrinsicWidth 與 getIntrinsicHeight 取得,但並非所有 Drawable 都具備固有寬高。
6.2 Drawable 的分類
6.2.1 BitmapDrawable
BitmapDrawable 表示一張圖片,其 XML 常用屬性如下:
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="https://schemas.android.com/apk/res/android" android:src="@mipmap/ic_launcher" android:antialias="true|false" android:dither="true|false" android:filter="true|false" android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal" android:mipMap="true|false" android:tileMode="disabled|clamp|repeat|mirror"/>
android:src 為圖片資源 ID,是必填屬性;android:antialias 為抗鋸齒;android:dither 為抖動效果、android:filter 為過濾效果,兩者都能優化顯示品質;android:gravity 用來定位圖片;android:mipMap 為紋理映射(較少用);android:tileMode 為平鋪模式,一旦啟用則 gravity 會被忽略,共有 disabled(關閉)、repeat(水平與垂直平鋪)、clamp(將邊緣像素擴展到周圍)、mirror(鏡面投影)四種。
NinePatchDrawable 表示 .9 格式圖片,XML 描述如下:
<?xml version="1.0" encoding="utf-8"?> <nine-patch xmlns:android="https://schemas.android.com/apk/res/android" android:src="@mipmap/ic_launcher" android:dither="true|false"/>
6.2.2 ShapeDrawable
ShapeDrawable 代表以顏色建構的圖形,可以是純色,也可以有漸層效果。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle|oval|line|ring"> <corners android:bottomLeftRadius="integer" android:bottomRightRadius="integer" android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" /> <gradient android:angle="integer" android:centerColor="integer" android:centerX="integer" android:centerY="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type="linear|radial|sweep" android:useLevel="true|false" /> <padding android:bottom="integer" android:left="integer" android:right="integer" android:top="integer" /> <size android:width="integer" android:height="integer" /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashGap="integer" android:dashWidth="integer" /> </shape>
其中 android:shape 指定形狀:rectangle(矩形)、oval(橢圓)、line(橫線)、ring(圓環),預設為矩形,而 line 與 ring 必須搭配 <stroke>;<corners> 為四角圓角,僅適用矩形;<gradient> 為漸層,與 <solid> 互斥;<solid> 為純色填充;<stroke> 為描邊;<padding> 為包含它的 View 留白;<size> 為 shape 的固有大小,並非最終顯示大小。
6.2.3 LayerDrawable
LayerDrawable 代表分層的 Drawable 集合,用來實現疊加效果。
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="https://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/ic_launcher" android:id="@+id/drawable" android:top="dimension" android:right="dimension" android:bottom="dimension" android:left="dimension"> </item> </layer-list>
一個 <layer-list> 可包含多個 item,每個 item 代表一個 drawable,後面的 item 會覆蓋前面的 item,透過合理分層即可做出特殊疊加效果。以下範例實作了類似微信文字輸入框的效果:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="https://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#0ac39e" /> </shape> </item> <item android:bottom="6dip"> <shape android:shape="rectangle"> <solid android:color="#ffffff" /> </shape> </item> <item android:bottom="1dp" android:left="1dp" android:right="1dp"> <shape android:shape="rectangle"> <solid android:color="#ffffff" /> </shape> </item> </layer-list>

6.2.4 StateListDrawable
主要用於設定可點擊 View 的背景,最常見的莫過於 Button,對應 <selector> 標籤,XML 格式如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://schemas.android.com/apk/res/android" android:constantSize="true|false" android:dither="true|false" android:variablePadding="true|false"> <item android:drawable="@mipmap/ic_launcher" android:state_pressed="true|false" android:state_focused="true|false" android:state_selected="true|false" android:state_checked="true|false" android:state_enabled="true|false" /> </selector>
item 的狀態繁多,以上僅列出常見幾種。android:constantSize 表示固有大小是否不隨狀態改變;android:dither 是否開啟抖動;android:variablePadding 表示 padding 是否隨狀態改變;state_pressed 為按下、state_focused 為取得焦點、state_selected 為選取、state_checked 為選中、state_enabled 為可用。預設的 item 一般放在 selector 最後一條且不附帶任何狀態。
6.2.5 LevelListDrawable
這是一個 Drawable 集合,每個 Drawable 都有對應的等級,LevelListDrawable 會依不同等級切換顯示的 Drawable,XML 語法如下:
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="https://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/ic_launcher" android:maxLevel="integer" android:minLevel="integer" /> </level-list>
等級範圍為 0~10000。作為 View 背景時可呼叫 setLevel 切換等級;作為 ImageView 前景時可透過 setImageLevel 切換。
6.2.6 TransitionDrawable
用來實現兩個 Drawable 間的淡入淡出效果,對應 <transition> 標籤,XML 語法如下:
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="https://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/ic_launcher" android:id="@+id/resource_name" android:top="dimension" android:bottom="dimension" android:left="dimension" android:right="dimension" /> </transition>
實例:切換 TextView 的背景
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="https://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/drawable1" /> <item android:drawable="@mipmap/drawable2" /> </transition>
<TextView android:background="@drawable/test9" android:layout_width="wrap_content" android:layout_height="wrap_content" />
TextView textView=(TextView)findViewById(R.id.test); TransitionDrawable drawable=(TransitionDrawable)textView.getBackground(); drawable.startTransition(1000);
6.2.7 InsetDrawable
可將其他 Drawable 內嵌到自己當中,對應 <inset> 標籤。當 View 希望背景比實際區域小時即可採用,XML 語法如下:
<?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="https://schemas.android.com/apk/res/android" android:drawable="@mipmap/ic_launcher" android:insetTop="dimension" android:insetBottom="dimension" android:insetLeft="dimension" android:insetRight="dimension"> </inset>
6.2.8 ScaleDrawable
依自身等級將指定 Drawable 縮放到一定比例,對應 <scale> 標籤,XML 語法如下:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="https://schemas.android.com/apk/res/android" android:drawable="@mipmap/ic_launcher" android:scaleGravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal" android:scaleHeight="percentage" android:scaleWidth="percentage"/>
使用時還需將 ScaleDrawable 的等級設為大於 0 且小於 10000,否則看不到效果。
ScaleDrawable scaleDrawable=(ScaleDrawable)textView.getBackground(); scaleDrawable.setLevel(1);
6.2.9 ClipDrawable
依當前等級裁剪另一個 Drawable,裁剪方向由 android:clipOrientation 與 android:gravity 共同控制,XML 屬性語法如下:
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="https://schemas.android.com/apk/res/android" android:clipOrientation="horizontal|vertical" android:drawable="@mipmap/ic_launcher" android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal" />
等級範圍同樣是 0~10000,0 表示完全裁剪,10000 表示不裁剪。
ClipDrawable clipDrawable=(ClipDrawable)textView.getBackground(); clipDrawable.setLevel(5000);
6.3 自訂 Drawable
自訂 Drawable 僅在特定情況下才會用到,實作時要特別注意 getIntrinsicWidth 與 getIntrinsicHeight 這兩個方法(書中 p262)。
minSdkVersion、maxSdkVersion、targetSdkVersion 與 target 差異
minSdkVersion、maxSdkVersion、targetSdkVersion 與 target 差異 Android 專案中常見的 SDK 版本設定包括 mi
Android 兩種程式化撥號方式:ACTION_CALL 與 ACTION_DIAL
想在 App 裡加一個撥號功能,Android 提供了兩條路: ACTION_CALL 直接撥出,ACTION_DIAL 則是開啟系統撥號盤並帶入號碼,由使用者決定是否按
Android 開發實戰:Service 詳解與後台音樂播放器的完整實現
Android 開發實戰:Service 詳解與後台音樂播放器 這一篇我們把 Service 這個組件徹底講透,並用它做一個能後台播放的音樂播放器,順手把 MediaPl
Android 教材|第三章 Android 介面事件處理(上)
Android 教材|第三章 Android 介面事件處理(上) 本文為傑瑞教育原創教材試讀單元。 前言 在 Android 應用開發中,除了介面編程之外,另一個重點