Android圖形-可繪製對象資源

參考官網博客:https://developer.android.google.cn/guide/topics/resources/drawable-resource#Bitmap

可繪製對象資源是一般概念,是指可在屏幕上繪製的圖形,以及可以使用 getDrawable(int) 等 API 檢索或者應用到具有 android:drawable 和 android:icon 等屬性的其他 XML 資源的圖形。共有多種不同類型的可繪製對象:

  • 位圖文件
    位圖圖形文件(.png、.jpg 或 .gif)。創建 BitmapDrawable。
  • 九宮格文件
    具有可拉伸區域的 PNG 文件,允許根據內容調整圖像大小 (.9.png)。創建 NinePatchDrawable。
  • 圖層列表
    管理其他可繪製對象陣列的可繪製對象。它們按陣列順序繪製,因此索引最大的元素繪製在頂部。創建 LayerDrawable。
  • 狀態列表
    此 XML 文件爲不同狀態引用不同位圖圖形(例如,按下按鈕時使用不同的圖像)。創建 StateListDrawable。
  • 級別列表
    此 XML 文件用於定義管理大量備選可繪製對象的可繪製對象,每個可繪製對象都分配有最大的備選數量。創建 LevelListDrawable。
  • 轉換可繪製對象
    此 XML 文件用於定義可在兩種可繪製對象資源之間交錯淡出的可繪製對象。創建 TransitionDrawable。
  • 插入可繪製對象
    此 XML 文件用於定義以指定距離插入其他可繪製對象的可繪製對象。當視圖需要小於視圖實際邊界的背景可繪製對象時,此類可繪製對象很有用。
  • 裁剪可繪製對象
    此 XML 文件用於定義對其他可繪製對象進行裁剪(根據其當前級別值)的可繪製對象。創建 ClipDrawable。
  • 縮放可繪製對象
    此 XML 文件用於定義更改其他可繪製對象大小(根據其當前級別值)的可繪製對象。創建 ScaleDrawable
  • 形狀可繪製對象
    此 XML 文件用於定義幾何形狀(包括顏色和漸變)。創建 ShapeDrawable。
    另請參閱動畫資源文檔,瞭解如何創建 AnimationDrawable。

注:顏色資源也可用作 XML 中的可繪製對象。例如,在創建狀態列表可繪製對象時,可以引用 android:drawable 屬性的顏色資源 (android:drawable="@color/green")。

位圖

位圖圖像。Android 支持以下三種格式的位圖文件:.png(首選)、.jpg(可接受)、.gif(不建議)。

您可以使用文件名作爲資源 ID 直接引用位圖文件,也可以在 XML 中創建別名資源 ID。

注:在構建過程中,可通過 aapt 工具自動優化位圖文件,對圖像進行無損壓縮。例如,不需要超過 256 色的真彩色 PNG 可通過調色板轉換爲 8 位 PNG。這樣產生的圖像質量相同,但所需內存更少。因此請注意,此目錄中的圖像二進制文件在構建時可能會發生變化。如果您計劃將圖像解讀爲比特流以將其轉換爲位圖,請改爲將圖像放在 res/raw/ 文件夾中,在那裏它們不會進行優化。

位圖文件

位圖文件是 .png、.jpg 或 .gif 文件。當您將這些文件保存到 res/drawable/ 目錄中時,Android 將爲它們創建 Drawable 資源。

文件位置:

res/drawable/filename.png(.png、.jpg 或 .gif)
文件名用作資源 ID。

編譯的資源數據類型:

指向 BitmapDrawable 的資源指針。

資源引用:

在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename

示例:

當圖像保存爲 res/drawable/myimage.png 後,此佈局 XML 會將圖像應用到視圖:

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

以下應用代碼將圖像作爲 Drawable 檢索:

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);

XML 位圖

XML 位圖是在 XML 中定義的資源,指向位圖文件。實際上是原始位圖文件的別名。XML 可以指定位圖的其他屬性,例如抖動和層疊。

注:您可以將<bitmap> 元素用作 <item> 元素的子項。例如,在創建狀態列表或圖層列表時,可以將 android:drawable 屬性從 <item> 元素中排除,並在其中嵌套用於定義可繪製項的 <bitmap>。

文件位置:

res/drawable/filename.xml
文件名用作資源 ID。

編譯的資源數據類型:

指向 BitmapDrawable 的資源指針。

資源引用:

在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename

語法

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    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"] />

元素:

定義位圖來源及其屬性。
屬性:

xmlns:android
字符串。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。這僅當 是根元素時才需要,當 嵌套在 內時不需要。
android:src
可繪製對象資源。必備。引用可繪製對象資源。
android:antialias
布爾值。啓用或停用抗鋸齒。
android:dither
布爾值。當位圖的像素配置與屏幕不同時(例如:ARGB 8888 位圖和 RGB 565 屏幕),啓用或停用位圖抖動。
android:filter
布爾值。啓用或停用位圖過濾。當位圖收縮或拉伸以使其外觀平滑時使用過濾。
android:gravity
關鍵字。定義位圖的重力。重力指示當位圖小於容器時,可繪製對象在其容器中放置的位置。
必須是以下一個或多個(用 ‘|’ 分隔)常量值:

說明
top 將對象放在其容器頂部,不改變其大小。
bottom 將對象放在其容器底部,不改變其大小。
left 將對象放在其容器左邊緣,不改變其大小。
right 將對象放在其容器右邊緣,不改變其大小。
center_vertical 將對象放在其容器的垂直中心,不改變其大小。
fill_vertical 按需要擴展對象的垂直大小,使其完全適應其容器。
center_horizontal 將對象放在其容器的水平中心,不改變其大小。
fill_horizontal 按需要擴展對象的水平大小,使其完全適應其容器。
center 將對象放在其容器的水平和垂直軸中心,不改變其大小。
fill 按需要擴展對象的垂直大小,使其完全適應其容器。這是默認值。
clip_vertical 可設置爲讓子元素的上邊緣和/或下邊緣裁剪至其容器邊界的附加選項。裁剪基於垂直重力:頂部重力裁剪上邊緣,底部重力裁剪下邊緣,任一重力不會同時裁剪兩邊。
clip_horizontal 可設置爲讓子元素的左邊和/或右邊裁剪至其容器邊界的附加選項。裁剪基於水平重力:左邊重力裁剪右邊緣,右邊重力裁剪左邊緣,任一重力不會同時裁剪兩邊。

android:mipMap
布爾值。啓用或停用 mipmap 提示。如需瞭解詳細信息,請參閱 setHasMipMap()。默認值爲 false。
android:tileMode
關鍵字。定義平鋪模式。當平鋪模式啓用時,位圖會重複。重力在平鋪模式啓用時將被忽略。
必須是以下常量值之一:

說明
disabled 不平鋪位圖。這是默認值。
clamp 當着色器繪製範圍超出其原邊界時複製邊緣顏色
repeat 水平和垂直重複着色器的圖像。
mirror 水平和垂直重複着色器的圖像,交替鏡像圖像以使相鄰圖像始終相接。

示例:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/icon"
    android:tileMode="repeat" />

九宮格

NinePatch 是一種 PNG 圖像,在其中可定義當視圖中的內容超出正常圖像邊界時 Android 縮放的可拉伸區域。此類圖像通常指定爲至少有一個尺寸設置爲 “wrap_content” 的視圖的背景,而且當視圖擴展以適應內容時,九宮格圖像也會擴展以匹配視圖的大小。Android 的標準 Button 小部件使用的背景就是典型的九宮格圖像,其必須拉伸以適應按鈕內的文本(或圖像)。

與普通位圖一樣,您可以直接引用九宮格文件,也可以從 XML 定義的資源引用。

如需有關如何創建包含可拉伸區域的九宮格文件的完整論述,請參閱 2D 圖形文件。
九宮格文件
文件位置:
res/drawable/filename.9.png
文件名用作資源 ID。
編譯的資源數據類型:
指向 NinePatchDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
示例:
當圖像保存爲 res/drawable/myninepatch.9.png 後,此佈局 XML 會將九宮格應用到視圖:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/myninepatch" />

XML 九宮格

XML 九宮格是在 XML 中定義的資源,指向九宮格文件。XML 可以爲圖像指定抖動。
文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 NinePatchDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:dither=["true" | "false"] />

元素:

定義九宮格來源及其屬性。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。
android:src
可繪製對象資源。必備。引用九宮格文件。
android:dither
布爾值。當位圖的像素配置與屏幕不同時(例如:ARGB 8888 位圖和 RGB 565 屏幕),啓用或停用位圖抖動。
示例:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/myninepatch"
    android:dither="false" />

圖層列表

LayerDrawable 是管理其他可繪製對象陣列的可繪製對象。列表中的每個可繪製對象按照列表的順序繪製,列表中的最後一個可繪製對象繪於頂部。

每個可繪製對象由單一 元素內的 元素表示。

文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 LayerDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</layer-list>

元素:

必備。這必須是根元素。包含一個或多個 元素。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。

定義要放在圖層可繪製對象中由其屬性定義的位置的可繪製對象。必須是 元素的子項。接受子 元素。
屬性:

android:drawable
可繪製對象資源。必備。引用可繪製對象資源。
android:id
資源 ID。此可繪製對象的唯一資源 ID。要爲此項新建資源 ID,請使用以下形式:"@+id/name"。加號表示應創建爲新 ID。可以使用此 ID 檢索和修改具有 View.findViewById() 或 Activity.findViewById() 的可繪製對象。
android:top
整型。頂部偏移(像素)。
android:right
整型。右邊偏移(像素)。
android:bottom
整型。底部偏移(像素)。
android:left
整型。左邊偏移(像素)。

默認情況下,所有可繪製項都會縮放以適應包含視圖的大小。因此,將圖像放在圖層列表中的不同位置可能會增大視圖的大小,並且有些圖像會相應地縮放。爲避免縮放列表中的項目,請在 元素內使用 元素指定可繪製對象,並且對某些不縮放的項目(例如 “center”)定義重力。例如,以下 定義縮放以適應其容器視圖的項目:
<item android:drawable="@drawable/image" />

爲避免縮放,以下示例使用重力居中的 元素:
<item> <bitmap android:src="@drawable/image" android:gravity="center" /> </item>
示例:
XML 文件保存在 res/drawable/layers.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/android_red"
        android:gravity="center" />
    </item>
    <item android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/android_green"
        android:gravity="center" />
    </item>
    <item android:top="20dp" android:left="20dp">
      <bitmap android:src="@drawable/android_blue"
        android:gravity="center" />
    </item>
</layer-list>

請注意,此示例使用嵌套的 元素爲每個具有“中心”重力的項目定義可繪製對象資源。這可確保沒有圖像會爲了適應容器的大小而縮放,因爲偏移圖像會造成大小調整。

此佈局 XML 會將可繪製對象應用到視圖:

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

結果導致一堆不斷偏移的圖像:

狀態列表

StateListDrawable 是在 XML 中定義的可繪製對象,它根據對象的狀態,使用多個不同的圖像來表示同一個圖形。例如,Button 小部件可以是多種不同狀態(按下、聚焦或這兩種狀態都不是)中的其中一種,而且可以利用狀態列表可繪製對象爲每種狀態提供不同的背景圖片。

您可以在 XML 文件中描述狀態列表。每個圖形由單一 元素內的 元素表示。每個 均使用各種屬性來描述應用作可繪製對象的圖形的狀態。

在每個狀態變更期間,將從上到下遍歷狀態列表,並使用第一個與當前狀態匹配的項目 —此選擇並非基於“最佳匹配”,而是選擇符合狀態最低條件的第一個項目。

文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 StateListDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

元素:

必備。這必須是根元素。包含一個或多個 元素。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。
android:constantSize
布爾值。如果可繪製對象報告的內部大小在狀態變更時保持不變,則值爲“true”(大小是所有狀態的最大值);如果大小根據當前狀態而變化,則值爲“false”。默認值爲 false。
android:dither
布爾值。值爲“true”時,將在位圖的像素配置與屏幕不同時(例如:ARGB 8888 位圖和 RGB 565 屏幕)啓用位圖的抖動;值爲“false”時則停用抖動。默認值爲 true。
android:variablePadding
布爾值。如果可繪製對象的內邊距應根據選擇的當前狀態而變化,則值爲“true”;如果內邊距應保持不變(基於所有狀態的最大內邊距),則值爲“false”。啓用此功能要求您在狀態變更時處理執行佈局,這通常不受支持。默認值爲 false。

定義要在某些狀態期間使用的可繪製對象,如其屬性所述。必須是 元素的子項。
屬性:

android:drawable
可繪製對象資源。必備。引用可繪製對象資源。
android:state_pressed
布爾值。如果在按下對象(例如觸摸/點按某按鈕)時應使用此項目,則值爲“true”;如果在默認的未按下狀態時應使用此項目,則值爲“false”。
android:state_focused
布爾值。如果在對象具有輸入焦點(例如當用戶選擇文本輸入時)時應使用此項目,則值爲“true”;如果在默認的非焦點狀態時應使用此項目,則值爲“false”。
android:state_hovered
布爾值。如果當光標懸停在對象上時應使用此項目,則值爲“true”;如果在默認的非懸停狀態時應使用此項目,則值爲“false”。通常,這個可繪製對象可能與用於“聚焦”狀態的可繪製對象相同。
此項爲 API 級別 14 新引入的配置。

android:state_selected
布爾值。如果在使用定向控件瀏覽(例如使用方向鍵瀏覽列表)的情況下對象爲當前用戶選擇時應使用此項目,則值爲“true”;如果在未選擇對象時應使用此項目,則值爲“false”。
當焦點 (android:state_focused) 不充分(例如,列表視圖有焦點但使用方向鍵選擇其中的項目)時,使用所選狀態。

android:state_checkable
布爾值。如果當對象可選中時應使用此項目,則值爲“true”;如果當對象不可選中時應使用此項目,則值爲“false”。(僅當對象可在可選中與不可選中小部件之間轉換時纔有用。)
android:state_checked
布爾值。如果在對象已選中時應使用此項目,則值爲“true”;如果在對象未選中時應使用此項目,則值爲“false”。
android:state_enabled
布爾值。如果在對象啓用(能夠接收觸摸/點擊事件)時應使用此項目,則值爲“true”;如果在對象停用時應使用此項目,則值爲“false”。
android:state_activated
布爾值。如果在對象激活作爲持續選擇(例如,在持續導航視圖中“突出顯示”之前選中的列表項)時應使用此項目,則值爲“true”;如果在對象未激活時應使用此項目,則值爲“false”。
此項爲 API 級別 11 新引入的配置。

android:state_window_focused
布爾值。如果當應用窗口有焦點(應用在前臺)時應使用此項目,則值爲“true”;如果當應用窗口沒有焦點(例如,通知欄下拉或對話框出現)時應使用此項目,則值爲“false”。

注:請記住,Android 將應用狀態列表中第一個與對象當前狀態匹配的項目。因此,如果列表中的第一個項目不含上述任何狀態屬性,則每次都會應用它,這就是默認值應始終放在最後的原因(如以下示例所示)。

示例:
XML 文件保存在 res/drawable/button.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

此佈局 XML 將狀態列表可繪製對象應用到按鈕:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />

級別列表

管理大量備選可繪製對象的可繪製對象,每個可繪製對象都分配有最大的備選數量。使用 setLevel() 設置可繪製對象的級別值會加載級別列表中 android:maxLevel 值大於或等於傳遞到方法的值的可繪製對象資源。

文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 LevelListDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<level-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/drawable_resource"
        android:maxLevel="integer"
        android:minLevel="integer" />
</level-list>

元素:

這必須是根元素。包含一個或多個 元素。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。

定義要在某特定級別使用的可繪製對象。
屬性:

android:drawable
可繪製對象資源。必備。引用要插入的可繪製對象資源。
android:maxLevel
整型。此項目允許的最高級別。
android:minLevel
整型。此項目允許的最低級別。
示例:

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/status_off"
        android:maxLevel="0" />
    <item
        android:drawable="@drawable/status_on"
        android:maxLevel="1" />
</level-list>

在此項目應用到 View 後,可通過 setLevel() 或 setImageLevel() 更改級別。

轉換可繪製對象

TransitionDrawable 是可在兩種可繪製對象資源之間交錯淡出的可繪製對象。

每個可繪製對象由單一 元素內的 元素表示。不支持超過兩個項目。要向前轉換,請調用 startTransition()。要向後轉換,則調用 reverseTransition()。

文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 TransitionDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</transition>

元素:

必備。這必須是根元素。包含一個或多個 元素。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。

定義要用作可繪製對象轉換一部分的可繪製對象。必須是 元素的子項。接受子 元素。
屬性:

android:drawable
可繪製對象資源。必備。引用可繪製對象資源。
android:id
資源 ID。此可繪製對象的唯一資源 ID。要爲此項新建資源 ID,請使用以下形式:"@+id/name"。加號表示應創建爲新 ID。可以使用此 ID 檢索和修改具有 View.findViewById() 或 Activity.findViewById() 的可繪製對象。
android:top
整型。頂部偏移(像素)。
android:right
整型。右邊偏移(像素)。
android:bottom
整型。底部偏移(像素)。
android:left
整型。左邊偏移(像素)。
示例:
XML 文件保存在 res/drawable/transition.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>

此佈局 XML 會將可繪製對象應用到視圖:

<ImageButton
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" />

以下代碼從第一個項目到第二個項目執行 500ms 的轉換:

ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);

插入可繪製對象

在 XML 文件中定義的以指定距離插入其他可繪製對象的可繪製對象。當視圖需要小於視圖實際邊界的背景時,此類可繪製對象很有用。

文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 InsetDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:insetTop="dimension"
    android:insetRight="dimension"
    android:insetBottom="dimension"
    android:insetLeft="dimension" />

元素:

定義插入可繪製對象。這必須是根元素。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。
android:drawable
可繪製對象資源。必備。引用要插入的可繪製對象資源。
android:insetTop
尺寸。頂部插入,表示爲尺寸值或尺寸資源
android:insetRight
尺寸。右邊插入,表示爲尺寸值或尺寸資源
android:insetBottom
尺寸。底部插入,表示爲尺寸值或尺寸資源
android:insetLeft
尺寸。左邊插入,表示爲尺寸值或尺寸資源

示例:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/background"
    android:insetTop="10dp"
    android:insetLeft="10dp" />

裁剪可繪製對象

在 XML 文件中定義的對其他可繪製對象進行裁剪(根據其當前級別)的可繪製對象。您可以根據級別以及用於控制其在整個容器中位置的重力,來控制子可繪製對象的裁剪寬度和高度。通常用於實現進度欄之類的項目。

文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 ClipDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<clip
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:clipOrientation=["horizontal" | "vertical"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                     "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                     "center" | "fill" | "clip_vertical" | "clip_horizontal"] />

元素:

定義裁剪可繪製對象。這必須是根元素。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。
android:drawable
可繪製對象資源。必備。引用要裁剪的可繪製對象資源。
android:clipOrientation
關鍵字。裁剪方向。
必須是以下常量值之一:

說明
horizontal 水平裁剪可繪製對象。
vertical 垂直裁剪可繪製對象。

android:gravity
關鍵字。指定可繪製對象中要裁剪的位置。
必須是以下一個或多個(用 ‘|’ 分隔)常量值:

說明
top 將對象放在其容器頂部,不改變其大小。當 clipOrientation 是 “vertical” 時,在可繪製對象的底部裁剪。
bottom 將對象放在其容器底部,不改變其大小。當 clipOrientation 是 “vertical” 時,在可繪製對象的頂部裁剪。
left 將對象放在其容器左邊緣,不改變其大小。這是默認值。當 clipOrientation 是 “horizontal” 時,在可繪製對象的右邊裁剪。這是默認值。
right 將對象放在其容器右邊緣,不改變其大小。當 clipOrientation 是 “horizontal” 時,在可繪製對象的左邊裁剪。
center_vertical 將對象放在其容器的垂直中心,不改變其大小。裁剪行爲與重力爲 “center” 時相同。
fill_vertical 按需要擴展對象的垂直大小,使其完全適應其容器。當 clipOrientation 是 “vertical” 時,不會進行裁剪,因爲可繪製對象會填充垂直空間(除非可繪製對象級別爲 0,此時它不可見)。
center_horizontal 將對象放在其容器的水平中心,不改變其大小。裁剪行爲與重力爲 “center” 時相同。
fill_horizontal 按需要擴展對象的水平大小,使其完全適應其容器。當 clipOrientation 是 “horizontal” 時,不會進行裁剪,因爲可繪製對象會填充水平空間(除非可繪製對象級別爲 0,此時它不可見)。
center 將對象放在其容器的水平和垂直軸中心,不改變其大小。當 clipOrientation 是 “horizontal” 時,在左邊和右邊裁剪。當 clipOrientation 是 “vertical” 時,在頂部和底部裁剪。
fill 按需要擴展對象的垂直大小,使其完全適應其容器。不會進行裁剪,因爲可繪製對象會填充水平和垂直空間(除非可繪製對象級別爲 0,此時它不可見)。
clip_vertical 可設置爲讓子元素的上邊緣和/或下邊緣裁剪至其容器邊界的附加選項。裁剪基於垂直重力:頂部重力裁剪上邊緣,底部重力裁剪下邊緣,任一重力不會同時裁剪兩邊。
clip_horizontal 可設置爲讓子元素的左邊和/或右邊裁剪至其容器邊界的附加選項。裁剪基於水平重力:左邊重力裁剪右邊緣,右邊重力裁剪左邊緣,任一重力不會同時裁剪兩邊。

示例:
XML 文件保存在 res/drawable/clip.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/android"
    android:clipOrientation="horizontal"
    android:gravity="left" />

以下佈局 XML 會將裁剪可繪製對象應用到視圖:

<ImageView
    android:id="@+id/image"
    android:background="@drawable/clip"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

以下代碼用於獲取可繪製對象,並增加裁剪量以便逐漸顯示圖像:

ImageView imageview = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel() + 1000);

增大級別可減少裁剪量並慢慢顯示圖像。此處的級別爲 7000:

注:默認級別爲 0,即完全裁剪,使圖像不可見。當級別爲 10,000 時,圖像不會裁剪,而是完全可見。

縮放可繪製對象

在 XML 文件中定義的更改其他可繪製對象大小(根據其當前級別)的可繪製對象。

文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 ScaleDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    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" />

元素:

定義縮放可繪製對象。這必須是根元素。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。
android:drawable
可繪製對象資源。必備。引用可繪製對象資源。
android:scaleGravity
關鍵字。指定縮放後的重力位置。
必須是以下一個或多個(用 ‘|’ 分隔)常量值:

說明
top 將對象放在其容器頂部,不改變其大小。
bottom 將對象放在其容器底部,不改變其大小。
left 將對象放在其容器左邊緣,不改變其大小。這是默認值。
right 將對象放在其容器右邊緣,不改變其大小。
center_vertical 將對象放在其容器的垂直中心,不改變其大小。
fill_vertical 按需要擴展對象的垂直大小,使其完全適應其容器。
center_horizontal 將對象放在其容器的水平中心,不改變其大小。
fill_horizontal 按需要擴展對象的水平大小,使其完全適應其容器。
center 將對象放在其容器的水平和垂直軸中心,不改變其大小。
fill 按需要擴展對象的垂直大小,使其完全適應其容器。
clip_vertical 可設置爲讓子元素的上邊緣和/或下邊緣裁剪至其容器邊界的附加選項。裁剪基於垂直重力:頂部重力裁剪上邊緣,底部重力裁剪下邊緣,任一重力不會同時裁剪兩邊。
clip_horizontal 可設置爲讓子元素的左邊和/或右邊裁剪至其容器邊界的附加選項。裁剪基於水平重力:左邊重力裁剪右邊緣,右邊重力裁剪左邊緣,任一重力不會同時裁剪兩邊。

android:scaleHeight
百分比。縮放高度,表示爲可繪製對象邊界的百分比。值的格式爲 XX%。例如:100%、12.5% 等。
android:scaleWidth
百分比。縮放寬度,表示爲可繪製對象邊界的百分比。值的格式爲 XX%。例如:100%、12.5% 等。
示例:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="80%"
    android:scaleWidth="80%" />

形狀可繪製對象

這是在 XML 中定義的一般形狀。

文件位置:
res/drawable/filename.xml
文件名用作資源 ID。
編譯的資源數據類型:
指向 GradientDrawable 的資源指針。
資源引用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
語法:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

元素:

形狀可繪製對象。這必須是根元素。
屬性:

xmlns:android
字符串。必備。定義 XML 命名空間,其必須是 “http://schemas.android.com/apk/res/android”。
android:shape
關鍵字。定義形狀的類型。有效值爲:

描述
“rectangle” 填充包含視圖的矩形。這是默認形狀。
“oval” 適應包含視圖尺寸的橢圓形狀。
“line” 跨越包含視圖寬度的水平線。此形狀需要 元素定義線寬。
“ring” 環形。

僅當 android:shape=“ring” 如下時才使用以下屬性:

android:innerRadius
尺寸。環內部(中間的孔)的半徑,以尺寸值或尺寸資源表示。
android:innerRadiusRatio
浮點型。環內部的半徑,以環寬度的比率表示。例如,如果 android:innerRadiusRatio=“5”,則內半徑等於環寬度除以 5。此值被 android:innerRadius 覆蓋。默認值爲 9。
android:thickness
尺寸。環的厚度,以尺寸值或尺寸資源表示。
android:thicknessRatio
浮點型。環的厚度,表示爲環寬度的比率。例如,如果 android:thicknessRatio=“2”,則厚度等於環寬度除以 2。此值被 android:innerRadius 覆蓋。默認值爲 3。
android:useLevel
布爾值。如果這用作 LevelListDrawable,則此值爲“true”。這通常應爲“false”,否則形狀不會顯示。

爲形狀產生圓角。僅當形狀爲矩形時適用。
屬性:

android:radius
尺寸。所有角的半徑,以尺寸值或尺寸資源表示。對於每個角,這會被以下屬性覆蓋。
android:topLeftRadius
尺寸。左上角的半徑,以尺寸值或尺寸資源表示。
android:topRightRadius
尺寸。右上角的半徑,以尺寸值或尺寸資源表示。
android:bottomLeftRadius
尺寸。左下角的半徑,以尺寸值或尺寸資源表示。
android:bottomRightRadius
尺寸。右下角的半徑,以尺寸值或尺寸資源表示。

注:(最初)必須爲每個角提供大於 1 的角半徑,否則無法產生圓角。如果希望特定角不要倒圓角,解決方法是使用 android:radius 設置大於 1 的默認角半徑,然後使用實際所需的值替換每個角,爲不希望倒圓角的角提供零(“0dp”)。

指定形狀的漸變顏色。
屬性:

android:angle
整型。漸變的角度(度)。0 爲從左到右,90 爲從上到上。必須是 45 的倍數。默認值爲 0。
android:centerX
浮點型。漸變中心的相對 X 軸位置 (0 - 1.0)。
android:centerY
浮點型。漸變中心的相對 Y 軸位置 (0 - 1.0)。
android:centerColor
顏色。起始顏色與結束顏色之間的可選顏色,以十六進制值或顏色資源表示。
android:endColor
顏色。結束顏色,表示爲十六進制值或顏色資源。
android:gradientRadius
浮點型。漸變的半徑。僅在 android:type=“radial” 時適用。
android:startColor
顏色。起始顏色,表示爲十六進制值或顏色資源。
android:type
關鍵字。要應用的漸變圖案的類型。有效值爲:

說明
“linear” 線性漸變。這是默認值。
“radial” 徑向漸變。起始顏色爲中心顏色。
“sweep” 流線型漸變。

android:useLevel
布爾值。如果這用作 LevelListDrawable,則此值爲“true”。

要應用到包含視圖元素的內邊距(這會填充視圖內容的位置,而非形狀)。
屬性:

android:left
尺寸。左內邊距,表示爲尺寸值或尺寸資源
android:top
尺寸。上內邊距,表示爲尺寸值或尺寸資源
android:right
尺寸。右內邊距,表示爲尺寸值或尺寸資源
android:bottom
尺寸。下內邊距,表示爲尺寸值或尺寸資源

形狀的大小。
屬性:

android:height
尺寸。形狀的高度,表示爲尺寸值或尺寸資源
android:width
尺寸。形狀的寬度,表示爲尺寸值或尺寸資源

注:默認情況下,形狀按照此處定義的尺寸按比例縮放至容器視圖的大小。在 ImageView 中使用形狀時,可通過將 android:scaleType 設置爲 “center” 來限制縮放。

用於填充形狀的純色。 屬性:

android:color
顏色。應用於形狀的顏色,以十六進制值或顏色資源表示。

形狀的筆劃中線。
屬性:

android:width
尺寸。線寬,以尺寸值或尺寸資源表示。
android:color
顏色。線的顏色,表示爲十六進制值或顏色資源。
android:dashGap
尺寸。短劃線的間距,以尺寸值或尺寸資源表示。僅在設置了 android:dashWidth 時有效。
android:dashWidth
尺寸。每個短劃線的大小,以尺寸值或尺寸資源表示。僅在設置了 android:dashGap 時有效。
示例:
XML 文件保存在 res/drawable/gradient_box.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

此佈局 XML 會將形狀可繪製對象應用到視圖:

<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

此應用代碼將獲取形狀可繪製對象,並將其應用到視圖:

Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章