Drawable資源
Drawable資源是對圖像的一個抽象,你可以通過getDrawable(int)得到並繪製到屏幕上。這裏有幾種不同類型的Drawable:
Bitmap File
一個Bitmap圖像文件(.png、.jpg或.gif)。BitmapDrawable。
Nine-Patch File
一個帶有伸縮區域的PNG文件,可以基於content伸縮圖片(.9.png)。NinePatchDrawable。
State List
一個XML文件,爲不同的狀態引用不同的Bitmap圖像(例如,當按鈕按下時使用不同的圖片)。StateListDrawable。
Color
定義在XML中的資源,指定一個矩形(圓角可以有)的顏色。PaintDrawable。
Shape
一個XML文件,定義了一個幾何形狀,包括顏色和漸變。ShapeDrawable。
AnimationDrawable資源的說明在【Animation資源】文章中。
Bitmap File
基本的Bitmap圖像。Android支持幾種不同格式的Bitmap文件:.png(最佳)、.jpg(可接受)、.gif(不要)。
注意:Bitmap文件可能會被aapt工具進行無損圖像壓縮優化。例如,一個真彩色的PNG(不超過256色)可能會被轉換成一個帶有顏色板的8位PNG。這樣做能保證圖片質量一樣,但減少內存佔用。因此,需要了解的是放在這個文件夾下的二進制圖像在編譯時可能會發生變更。如果你打算以位流方式讀取圖像來轉化成Bitmap的話,可以把它們放到res/raw文件中,在這裏,它們不會被優化。
File Location:
res/drawable/filename.png (.png, .jpg, 或.gif)
文件名會被當作資源ID使用。
Complied Resource Datatype:
指向BitmapDrawable的資源指針。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Example:
在res/drawable/myimage.png位置保存了一張圖片,在Layout XML中可以應用這個圖片到一個View上:
<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);
Nine-Patch File
NinePatch是一種PNG圖像,可以定義拉伸區域,當View的content超出圖像邊界的話,Android會拉伸它。典型用法是把這個圖像設置爲View的背景,而這個View至少有一個尺寸設置爲“wrap_content”,當這個View變大來容納content時,Nine-Patch圖像也會拉伸來匹配View的大小。
File Location:
res/drawable/filename.9.png
文件名將被當作資源ID使用。
Complied Resource Datatype:
指向NinePatchDrawable的資源指針。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Example:
在res/drawable/myninepatch.9.png位置保存了一張圖片,在Layout XML中可以應用這個圖片到一個View上:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/myninepatch" />
State List
StateListDrawable是定義在XML中的Drawable對象,能根據狀態來呈現不同的圖像。例如,Button存在多種不同的狀態(pressed、focused或other),使用StateListDrawable,你可以爲Button的每個狀態提供不同的按鈕圖像。
你可以在XML文件中描述狀態列表。在<selector>元素裏的每個<item>代表每個圖像。每個<item>使用不同的特性來描述使用的時機。
當每次狀態改變時,StateList都會從上到下遍歷一次,第一個匹配當前狀態的item將被使用——選擇的過程不是基於“最佳匹配”,只是符合state的最低標準的第一個item。
File Location
Res/drawable/filename.xml
文件名將被當作資源ID使用。
Complied Resource Datatype:
指向StateListDrawable的資源指針。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Syntax:
<?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_selected=["true" | "false"]
android:state_active=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
Elements:
<selector>
必須。必須是根元素。可以包含一個或多個<item>元素。
Attributes:
xmlns:android
String。必須。定義XML的命名空間,必須是
“http://schemas.android.com/apk/res/android”。
android:constantSize
Boolean。“true”表示隨着狀態變化,Drawable的大小保持不變(所有狀態中最大的size);“false”表示大小會變化。默認是false。
android:dither
Boolean。“true”表示當Bitmap和屏幕的不是相同的像素設定時支持Bitmap抖動(例如,ARGB 8888的Bitmap和RGB 565的屏幕);“false”表示不支持。默認是“true”。
android:variablePadding
Boolean。“true”表示Drawable的Padding可以變化;“false”表示Padding保持相同(所有狀態的最大Padding)。使能這一特徵需要在狀態變化時處理Layout,一般都不支持。默認值是false。
<item>
定義特定狀態的Drawable,通過它的特性指定。必須是<selector>的子元素。
Attributes:
android:drawable
Drawable資源。必須。指向一個Drawable資源。
android:state_pressed
Boolean。“true”表示按下狀態使用(例如按鈕按下);“false”表示非按下狀態使用。
android:state_focused
Boolean。“true”表示聚焦狀態使用(例如使用滾動球/D-pad聚焦Button);“false”表示非聚焦狀態使用。
android:state_selected
Boolean。“true”表示選中狀態使用(例如Tab打開);“false”表示非選中狀態使用。
android:state_checkable
Boolean。“true”表示可勾選狀態時使用;“false”表示非可勾選狀態使用。(只對能切換可勾選—非可勾選的構件有用。)
android:state_checked
Boolean。“true”表示勾選狀態使用;“false”表示非勾選狀態使用。
android:state_enabled
Boolean。“true”表示可用狀態使用(能接收觸摸/點擊事件);“false”表示不可用狀態使用。
android:window_focused
Boolean。“true”表示應用程序窗口有焦點時使用(應用程序在前臺);“false”表示無焦點時使用(例如Notification欄拉下或對話框顯示)。
注意:記住一點,StateList中第一個匹配當前狀態的item會被使用。因此,如果第一個item沒有任何狀態特性的話,那麼它將每次都被使用,這也是爲什麼默認的值必須總是在最後(如下面的例子所示)。
Example:
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:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
Layout XML將這個Drawable應用到一個View上:
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/button" />
Color
定義在XML中的color,可以當作Drawable使用,來填充矩形區域(圓角可以有)。這種Drawable的行爲很像是顏色填充。
注意:Color Drawable是一種簡單的資源,可以使用name特性來引用其值(不再是XML文件的名)。因此,你可以在一個XML文件中的<resources>元素下添加多個Color Drawable。
File Location:
res/drawable/filename.xml
文件名隨意。元素的name將會當作資源ID使用。
Complied Resource Datatype:
指向PaintDrawable資源的指針。
Resource Reference:
R.drawable.color_name(Java)
@[package:]drawable/color_name(XML)
Syntax:
Elements:
<resources>
必須。必須是根節點。
沒有特性。
<drawable>
一個color Drawable。其值可以是任何有效的十六進制顏色值或者Color資源。Color值總是以“#”開頭,後面緊跟Alpha-Red-Green-Blue信息,格式是:#RGB、#ARGB或者#AARRGGBB。
Attributes:
name
String。必須。Color的名字。這個名字將被當作資源ID使用。
Example:
XML文件保存在res/drawable/color.xml。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="solid_red">#f00</drawable>
<drawable name="solid_blue">#0000ff</drawable>
</resources>
Layout XML將會把這個Color Drawable應用到一個View上:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/solid_blue" />
代碼中獲取Color Drawable並應用到View上:
Resources res = getResources();
Drawable redDrawable = res.getDrawable(R.drawable.solid_red);
TextView tv = (TextView) findViewByID(R.id.text);
tv.setBackground(redDrawable);
Shape
定義在XML中的幾何形狀。
File Location:
res/drawable/filename.xml
文件名將被當作資源ID使用。
Complied Resource Datatype:
指向ShapeDrawable的資源指針。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:usesLevel=["true" | "false"] />
android:color="color" />
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
</shape>
Elements:
<shape>
必須。必須是根元素。
Attributes:
android:shape
Keyword。定義Shape的類型。有效的值包括:
Value |
Desciption |
|
矩形。默認形狀。 |
|
橢圓。 |
|
水平直線。需要<stroke>元素定義線的寬度。 |
|
環形。 |
接下來的特性只能在android:shape=”ring”時使用:
android:innerRadius
Dimension。內環的半徑。
android:innerRadiusRatio
Float。以環的寬度比率來表示內環的半徑。例如,如果android:innerRadiusRatio=”5”,內環半徑等於環的寬度除以5。這個值可以被android:innerRadius覆蓋。默認值是9。
android:thickness
Dimension。環的厚度。
android:thicknessRatio
Float。以環的寬度比率來表示環的厚度。例如,如果android:thicknessRatio=”2”,厚度就等於環的寬度除以2。這個值可以被android:thickness覆蓋。默認值是3。
android:useLevel
Boolean。“true”表示可以當作LevelListDrawable使用。一般都爲“false”。
<gradient>
爲Shape指定漸變色。
Attributes:
android:angle
Integer。漸變色的角度值。0表示從左到右,90表示從下到上。必須是45的倍數,默認是0。
android:centerX
Float。漸變色中心的X相對位置(0-1.0)。當android:type=”linear”時無效。
android:centerY
Float。漸變色中心的Y相對位置(0-1.0)。當android:type=”linear”時無效。
android:centerColor
Color。可選的顏色,出現在start和end顏色之間。
android:endColor
Color。end顏色。
android:gradientRadius
Float。漸變色的半徑。當android:type=”radial”時有效。
android:startColor
Color。start顏色。
android:type
Keyword。漸變色的樣式。有效值爲:
Value |
Description |
|
線性漸變,默認值。 |
|
環形漸變。start顏色是處於中間的顏色。 |
|
sweep漸變 |
android:useLevel
Boolean。“true”表示可以當作LevelListDrawable使用。
<solid>
填充shape的單一色。
Attributes:
android:color
Color。這個顏色會應用到shape上。
<stroke>
shape的線形。
Attributes:
android:width
Dimension。線的厚度。
android:color
Color。線的顏色。
android:dashGap
Dimension。間斷線間的距離。僅在android:dashWidth設定時有效。
android:dashWidth
Dimension。間斷線的大小。僅在android:dashGap設定時有效。
<padding>
內部View元素的邊距。
Attributes:
android:left
Dimension。左內邊距。
android:top
Dimension。上內邊距。
android:right
Dimension。右內邊距。
android:bottom
Dimension。下內邊距。
<corners>
爲shape創建圓角。當shape是一個矩形時有效。
Attributes:
android:radius
Dimension。圓角的半徑。會被下面的特性覆蓋。
android:topLeftRadius
Dimension。左上圓角半徑。
android:topRightRadius
Dimension。右上圓角半徑。
android:bottomLeftRadius
Dimension。左下圓角半徑。
android:bottomRightRadius
Dimension。右下圓角半徑。
Examples:
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>
Layout XML將被當作ShapeDrawable應用到一個View上:
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
代碼中也可以獲得ShapeDrawable,然後應用到View上:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);