縮放可繪製資源
定義在XML中的一個可繪製資源,它能夠基於自己當前的層級來改變另一個可繪製資源的尺寸。
檔案位置(FILE LOCATION):
res/drawable/filename.xml
檔案名稱被用作資源ID。
被編譯資源的資料類型(COMPILED RESOURCE DATATYPE):
資源指向一個ScaleDrawable對象。
資源引用(RESOURCE REFERENCE)
在Java代碼中:R.drawable.filename
在XML中:@[package:]drawable/filename
文法(SYNTAX):
<?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"/>
元素(ELEMENTS):
<scale>
定義縮放可繪製資源。它必須是根項目。
屬性(ATTRIBUTES):
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 |
額外的選項,它能夠把它的容器的上下邊界,設定為子物件的上下邊緣的裁剪邊界。裁剪要基於對象垂直重力設定:如果重力設定為top,則裁剪下邊,如果設定為bottom,則裁剪上邊,否則則上下兩邊都要裁剪。 |
| clip_horizontal |
額外的選項,它能夠把它的容器的左右邊界,設定為子物件的左右邊緣的裁剪邊界。裁剪要基於對象垂直重力設定:如果重力設定為right,則裁剪左邊,如果設定為left,則裁剪右邊,否則則左右兩邊都要裁剪。 |
android:scaleHeight
百分比。它指定了縮放的高度。用可繪製資源的上下邊界之間高度的百分比來表示。格式是xx%,例如:100%,12.5%,等。
android:scaleWidth
百分比。它指定了縮放的寬度。用可繪製資源的左右邊界之間寬度的百分比來表示。格式是xx%,例如:100%,12.5%等。
例子(EXAMPLE):
<?xml version="1.0" encoding="utf-8"?>
<scalexmlns: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中的幾何形狀。
檔案位置(FILE LOCATION):
res/drawable/filename.xml
檔案被用作資源ID。
被編譯資源的資料類型(COMPILED RESOURCE DATATYPE):
資源指向一個GradientDrawable對象
資源引用(RESOURCE REFERENCE):
在Java代碼中:R.drawable.filename
在XML中:@[package]drawable/filename
文法(SYNTAX):
<?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="integer"
android:centerY="integer"
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>
元素(ELEMENTS):
<shape>
定義一個幾何形狀資源,它必須是根項目。
屬性(ATTRIBUTES):
xmlns:android
字串值,必須的。它定義了XML的命名空間,必須是:http://schemas.android.com/apk/res/android
android:shape
關鍵屬性,它定義了形狀的類型,有效值如下:
| 值 |
說明 |
| rectangle |
在View對象中填充一個矩形。這是預設的形狀。 |
| oval |
適合View對象大小的橢圓形。 |
| line |
貫穿View對象寬度的一個條水平線。這個形狀需要用<stroke>元素來定義線的寬度。 |
| ring |
一個圓環 |
下列屬性只在android:shape=”ring”時使用:
android:innerRadiux
尺寸值,它用尺寸值或尺寸資源指定圓環內部的半徑(指中間的圓孔的半徑)。
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或者讓形狀不可見。
<corners>
給幾何形狀建立一個圓角,它只應用與矩形。
屬性(ATTRIBUTES):
android:radius
尺寸值,用尺寸值或尺寸資源(dimension resource)定義所有圓角的半徑,它會被下列屬性定義的每個角的值所覆蓋。
android:topLeftRadius
尺寸值,它用尺寸值或尺寸資源定義左上方的圓角半徑。
android:topRightRadius
尺寸值,它用尺寸值或尺寸資源定義右上方的圓角半徑。
android:bottomLeftRadius
尺寸值,它用尺寸值或尺寸資源定義左下角的圓角半徑。
android:bottomRightRadius
尺寸值,它用尺寸值或尺寸資源定義右下角的圓角半徑。
注意:最初必須給每個角提供一個比1大的圓角半徑,否則角不會被圓化。如果要想讓角沒有圓弧,一種工作方式是使用android:radius屬性來設定預設的大於1的圓角半徑,然後對於不想要圓弧的角,設定對應的屬性為0dp,這樣這個角就不會有圓弧了。
<gradient>
為幾何形狀指定漸層色。
屬性(ATTRIBUTES):
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。
<padding>
它定義了與包含形狀的View元素相適應的邊距(這個邊距是View對象內容的位置,而不是形狀的位置)。
屬性(ATTRIBUTES):
android:left
尺寸值,它用尺寸值或尺寸資源定義了左邊距。
android:top
尺寸值,它用尺寸值或尺寸資源定義了上邊距。
android:right
尺寸值,它用尺寸值或尺寸資源定義了右邊距。
android:bottom
尺寸值,它用尺寸值或尺寸資源定義了下邊距。
<size>
它定義了幾何形狀的大小。
屬性(ATTRIBUTES):
android:height
尺寸值,它用尺寸值或尺寸資源定義了幾何形狀的高度。
android:width
尺寸值,它用尺寸值或尺寸資源定義了幾何形狀的寬度。
注意:預設情況下,幾何形狀定義的尺寸會被縮放到與它的容器相適應的尺寸。在ImageView對象中使用幾何形狀時,能夠通過把android:scaleType屬性設定為center來限制縮放。
<solid>
定義填充幾何形狀的實體顏色。
屬性(ATTRIBUTES):
android:color
顏色值。它指定應用與幾何形狀的顏色值。用十六進位的顏色值或顏色資源來表示。
<stroke>
給形狀定義一個虛線。
屬性(ATTRIBUTES):
android:width
尺寸值。它指定了線的厚度,用尺寸值或尺寸資源來表示。
android:color
顏色值。它指定了線的顏色,用十六進位的顏色值或顏色資源來表示。
android:dashGap
尺寸值,它定義了破折線之間的距離,用尺寸值或尺寸資源來表示。只有設定了android:dashWidth屬性時,這個屬性才有效。
android:dashWidth
尺寸值,它定義了每個破折線的尺寸,用尺寸值或尺寸資源來表示。只有設定了android:dashGap屬性時,這個屬性才有效。
例子(EXAMPLE):
XML檔案被儲存在res/drawable/gradient_box.xml中
<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<paddingandroid:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp"/>
<cornersandroid:radius="8dp"/>
</shape>
以下是布局XML把這個形狀資源應用於一個View元素的方法:
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
以下是代碼擷取形狀資源,並把其應用於View對象的方法:
Resources res =getResources();
Drawable shape = res.getDrawable(R.drawable.gradient_box);
TextView tv =(TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
摘自 FireOfStar的專欄