Android應用資源—繪制資源類型(Drawable)(六)

縮放可繪制資源

定義在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的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *