Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android Drawable 九大類型標籤用法詳解

編輯:Android 開發實例

1. BitmapDrawable

在 drawable 目錄下建立如下檔案:

<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:dither="true"
    android:filter="true"
    android:gravity="center"
    android:mipMap="false"
    android:src="@drawable/ic_launcher"
    android:tileMode="disabled" />

各屬性說明(以下排版僅為方便閱讀,不可直接複製使用):

  • antialias:開啟抗鋸齒,讓圖片邊緣更平滑。
  • dither:抖動效果,讓高品質圖片在低品質螢幕上仍有不錯的呈現。
  • filter:開啟過濾以保持顯示品質。
  • gravity:位置控制,可用「|」組合。top / bottom / left / right 對齊且不縮放;center_vertical / center_horizontal 單軸居中;fill_vertical / fill_horizontal 單軸填滿;center 居中;fill 填滿整個容器(預設值);clip_vertical / clip_horizontal 為裁剪的附加選項,較少使用。
  • mipMap:紋理映射,一般設為 false。
  • src:圖片資料來源。
  • tileMode:平鋪模式。disabled 為預設(關閉平鋪);repeat 為水平與垂直雙向平鋪;mirror 為鏡面投影;clamp 則把四周像素向外擴散。

2. ShapeDrawable:純色與漸變

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:bottomLeftRadius="1px"
        android:bottomRightRadius="1px"
        android:radius="2px"
        android:topLeftRadius="1px"
        android:topRightRadius="1px" />

    <gradient
        android:angle="5"
        android:centerColor="#ff789434"
        android:centerX="x"
        android:centerY="y"
        android:endColor="#34234324"
        android:gradientRadius="55dp"
        android:startColor="#ff783247"
        android:type="linear"
        android:useLevel="true" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

    <size android:width="70dp" android:height="70dp" />
    <solid android:color="#fffff333" />

    <stroke
        android:width="30dp"
        android:color="#33333333"
        android:dashGap="4dp"
        android:dashWidth="4dp" />
</shape>

<shape>:定義圖形形狀,有 rectangle(矩形)、oval(橢圓)、line(橫線)、ring(圓環)四種,預設矩形。使用 line 與 ring 時必須透過 stroke 指定線寬與顏色,否則顯示會異常。設為 ring 時另有五個專屬屬性:innerRadius(內半徑,與 innerRadiusRatio 同時存在時以它為準)、thickness(環的厚度,即外半徑減內半徑)、innerRadiusRatio(內半徑佔整體寬度比例,預設 9)、thicknessRatio(厚度佔整體比例,預設 3,算法為厚度 = 寬度 / n)、useLevel(一般為 false,除非當作 LevelListDrawable 使用)。

<corners>:四個角的角度,僅適用矩形,單位 px。radius 可統一設定四角但優先級較低;topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius 分別對應左上、右上、左下、右下。

<gradient>:漸變填充,與 solid 互斥。angle 為漸變角度(必須是 45 的倍數,預設 0);centerX / centerY 為漸變中心座標;startColor / centerColor / endColor 為起始、中間與結束色;gradientRadius 為漸變半徑;type 有 linear(線性,預設)、radial(徑向)、sweep(掃描線)三種;useLevel 一般為 false,僅在 type 為 radial 時才需要。

<solid>:用 android:color 指定純色填充。

<stroke>:描邊。width 為線寬,color 為顏色,dashWidth 為虛線線段寬度,dashGap 為線段間距。

<padding>:內邊距,用法與 View 的 padding 相同,含 top / bottom / left / right 四個屬性。

<size>:width 與 height 定義尺寸,行為類似圖片本身的寬高,套用時會視情況縮放。

3. LayerDrawable:圖層疊加

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/item1"
        android:bottom="1dp"
        android:drawable="@drawable/ic_launcher"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">
    </item>
</layer-list>

一個 layer-list 可包含多個 item,每個 item 代表一個 drawable。上下左右四個屬性是相對於 view 的偏移量(像素),可用 drawable 直接引用資源,也可以在 item 內部自行定義。預設所有 drawable 都會被縮放到 view 的大小,且後面(下方)的圖層會覆蓋前面的。合理的分層能做出不少特殊疊加效果。

4. StateListDrawable:狀態選擇器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android"
    android:constantSize="true"
    android:dither="true"
    android:variablePadding="true">

    <item
        android:drawable="@drawable/ic_launcher"
        android:state_pressed="true"
        android:state_focused="true"
        android:state_hovered="true"
        android:state_selected="true"
        android:state_checkable="true"
        android:state_checked="true"
        android:state_enabled="true"
        android:state_activated="true"
        android:state_window_focused="true">
    </item>
</selector>

constantSize 表示尺寸是否固定不隨狀態改變(預設 false);dither 建議開啟以改善低解析度螢幕的顯示;variablePadding 表示內邊距是否隨狀態改變。可用的狀態包含 pressed(按下未鬆開)、focused(已取得焦點)、hovered(游標懸停,4.0 之後)、selected、checkable、checked、enabled、activated 與 window_focused(App 是否在前台)。

關鍵規則:多個 item 時程式會由上到下逐一比對,先命中者生效(不是最佳匹配)。而沒有任何狀態說明的 item 可被任何狀態匹配,所以預設項必須放在最後一條,否則下面的選項永遠不會生效。

5. LevelListDrawable:依等級切換

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

集合中每個 drawable 都有一個等級區間。當它作為 view 背景時,可呼叫 Drawable 的 setLevel() 切換,ImageView 則可用 setImageLevel()。等級範圍是 0 到 10000,0 為預設值。

6. TransitionDrawable:淡入淡出

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

把它設為背景:

<TextView
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/transition_drawable" />

再在程式碼中觸發:

TextView textView = (TextView) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) textView.getBackground();
drawable.startTransition(1000);

7. InsetDrawable:內嵌並留白

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

把一個 Drawable 內嵌在自己內部並於四周留出間距,常用於「希望背景比實際區域小一點」的場合。

8. ScaleDrawable:縮放

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="https://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_launcher"
    android:scaleGravity="center"
    android:scaleHeight="50%"
    android:scaleWidth="50%" >
</scale>

使用時必須設定等級,否則不會生效:

View testScale = findViewById(R.id.test_scale);
ScaleDrawable testScaleDrawable = (ScaleDrawable) testScale.getBackground();
testScaleDrawable.setLevel(1);   // 範圍 1~10000,只要非 0 即可

9. ClipDrawable:裁剪

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="https://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal"
    android:drawable="@+id/icon"
    android:gravity="top" >
</clip>

gravity 與 clipOrientation 搭配決定從哪一側開始裁剪:

  • top:置頂;垂直裁剪時從底部開始裁。
  • bottom:置底;垂直裁剪時從頂部開始裁。
  • left:置左(預設);水平裁剪時從右側開始裁。
  • right:置右;水平裁剪時從左側開始裁。
  • center_vertical / center_horizontal:單軸居中,該軸向裁剪時兩端同時收。
  • fill_vertical / fill_horizontal:單軸填滿,僅當等級為 0(完全不可見)時才有裁剪行為。
  • center:居中,垂直裁剪時上下同減,水平裁剪時左右同減。
  • fill:填滿整個 drawable,等級為 0 時有效。
  • clip_vertical / clip_horizontal:附加選項,標示裁剪方向。

同樣需要在程式碼中設定等級:

ImageView testClip = (ImageView) findViewById(R.id.test_clip);
ClipDrawable testClipDrawable = (ClipDrawable) testClip.getDrawable();
// 等級 0 表示完全裁剪,10000 表示完全不裁剪
testClipDrawable.setLevel(5000);

掌握這幾種標籤,多數常見的圓角按鈕、漸層背景、進度條與狀態切換效果,都能純靠 XML 完成,不必額外寫繪製程式碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved