Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Shape Drawable 詳解:用 XML 畫出圓角、漸變、虛線與形狀

編輯:關於 Android 程式設計

Shape Drawable 可以用 XML 實現各種形狀圖形與顏色漸變效果。相比 PNG 圖片,它佔用的空間更小;相比自定義 View,實現起來簡單得多。這篇筆記把四種形狀與六個常用標籤一次講清楚。

怎麼畫?

在 res/drawable/ 目錄下新建一個 XML 資源文件,根節點是 <shape>。語法相對繁瑣,下面是一份覆蓋大部分參數的註釋版本,建議先跳過,看完後面的分類講解再回頭對照:

<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- 圓角 -->
    <corners
        android:radius="5dip"
        android:topLeftRadius="5dip"
        android:topRightRadius="5dip"
        android:bottomLeftRadius="5dip"
        android:bottomRightRadius="5dip" />

    <!-- 漸變填充 -->
    <gradient
        android:angle="0"
        android:startColor="#000"
        android:centerColor="#000"
        android:endColor="#FFF"
        android:centerX="0.5"
        android:centerY="0.5"
        android:type="linear"
        android:gradientRadius="20dip" />

    <!-- 內邊距:控件內容與 shape 邊緣的距離 -->
    <padding
        android:left="5dip"
        android:top="15dip"
        android:right="5dip"
        android:bottom="5dip" />

    <!-- 尺寸 -->
    <size
        android:width="50dip"
        android:height="10dip" />

    <!-- 純色背景 -->
    <solid android:color="@android:color/white" />

    <!-- 邊框,後兩個屬性用於虛線 -->
    <stroke
        android:width="5dip"
        android:color="#0000FF"
        android:dashWidth="1dip"
        android:dashGap="2dip" />
</shape>

shape 的四種形狀

一共四種:rectangle(長方形,不指定時的默認值)、oval(橢圓)、line(線)、ring(環形)。

其中 line 與 ring 各有幾個特殊點需要注意。

line 形狀下 solid 標籤會失效,必須改用 stroke 來指定顏色與寬度;stroke 若不指定 color,默認是黑色。

ring 形狀下有四個專屬屬性:innerRadius 是內圓半徑;innerRadiusRatio 是比率(shape 寬度 / 內半徑),默認 9,與 innerRadius 同時存在時以前者為準;thickness 是圓環厚度;thicknessRatio 是比率(shape 寬度 / 圓環厚度),默認 3,與 thickness 同時存在時同樣以前者為準;useLevel 一般設為 false,否則可能導致不顯示。

corners 標籤:圓角

作用是指定長方形四角的圓滑度,圓角矩形就是靠它實現的。

bottomLeftRadius 左下角、bottomRightRadius 右下角、topLeftRadius 左上角、topRightRadius 右上角。

radius 一次設置四個角,寫了它就不用寫上面四個了——但它的優先級較低,會被單角設置覆蓋。

gradient 標籤:漸變

作用很直白:讓圖形呈現顏色過渡。

angle 是顏色變換角度,默認 0,取值必須是 45 的倍數。0 表示從左到右,90 表示從下到上。

startColor、centerColor、endColor 分別是起始色、中間色、結束色。

centerX 與 centerY 是指定中心點坐標,取值 0.0f 到 1.0f 之間,例如 android:centerX="0.5f" 表示 X 方向的中點。

type 是漸變類型,有三種取值:linear 線性漸變(顏色沿直線過渡)、radial 放射漸變(從圓心向邊緣變化)、sweep 掃描式漸變(類似雷達掃描)。

gradientRadius 需與 android:type="radial" 搭配使用,指定 shape 的半徑。

三種漸變對應的布局與資源文件:

<!-- 布局 -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dip"
        android:background="@drawable/shape_rectangle_linear"
        android:text="linear" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dip"
        android:background="@drawable/shape_rectangle_radial"
        android:text="radial" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dip"
        android:background="@drawable/shape_rectangle_sweep"
        android:text="sweep" />
</LinearLayout>
<!-- shape_rectangle_linear.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dip">
        <gradient
            android:angle="0"
            android:startColor="#FFFFFF"
            android:centerColor="#FF4081"
            android:endColor="#000000"
            android:centerX="0.5"
            android:centerY="0.5"
            android:type="linear"
            android:useLevel="false" />
    </corners>
</shape>
<!-- shape_rectangle_radial.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dip">
        <gradient
            android:angle="0"
            android:startColor="#000000"
            android:centerColor="#FF4081"
            android:endColor="#FFFFFF"
            android:centerX="0.5"
            android:centerY="0.5"
            android:type="radial"
            android:gradientRadius="150dip"
            android:useLevel="false" />
    </corners>
</shape>
<!-- shape_rectangle_sweep.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dip">
        <gradient
            android:angle="0"
            android:startColor="#000000"
            android:centerColor="#FF4081"
            android:endColor="#FFFFFF"
            android:centerX="0.5"
            android:centerY="0.5"
            android:type="sweep"
            android:useLevel="false" />
    </corners>
</shape>

幾點補充:solid 標籤會與 gradient 衝突,並覆蓋 gradient 配置的顏色,兩者只能選其一;gradientRadius 只在 type 為 radial 時生效。

padding 標籤:內邊距

作用是設置控件內容(通常是文字)與 shape 邊緣的距離,包含 bottom、left、right、top 四個方向。

<!-- shape_rectangle_padding.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dip">
        <solid android:color="#FF4081">
            <padding
                android:left="30dip"
                android:top="30dip"
                android:right="30dip"
                android:bottom="30dip" />
        </solid>
    </corners>
</shape>

size 標籤:尺寸

指定 shape 的寬高,但使用方式不同,效果差別很大。

當用控件的 background 屬性引用時,size 標籤基本無效,shape 會隨控件大小自由拉伸縮放。

當用 src 屬性引用時分兩種情況:shape 比控件小時,它會居中顯示在控件中間;shape 比控件大時,寬高會等比縮放,一直壓縮到能放進控件內並居中。

<!-- shape_rectangle_size.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dip">
        <solid android:color="#00ff00">
            <size android:width="200dip" android:height="100dp" />
        </solid>
    </corners>
</shape>

實務建議:用 src 引用 shape 處理起來比較麻煩,實際開發中很少有人這麼用,直接用 background 就好。

solid 標籤:純色背景

給圖形設置背景色。要注意它和 gradient 是互斥的,solid 會覆蓋 gradient 的顏色配置。

一個實用技巧:在 solid 的 color 屬性裡引用 color selector(selector.xml),就能實現點擊換色的效果,比寫多份 shape 文件省事得多。

stroke 標籤:邊框

給 shape 設置邊框,可以控制寬度、顏色,也能做成虛線。

width 是邊框寬度,color 是邊框顏色,dashWidth 是虛線中實線段的長度,dashGap 是虛線間隔的長度。

<!-- shape_rectangle_stroke.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dip">
        <solid android:color="#00ff00">
            <stroke
                android:width="5dip"
                android:color="#FF4081"
                android:dashWidth="10dip"
                android:dashGap="5dip" />
        </solid>
    </corners>
</shape>

實際開發中的兩個常見場景

場景一:列表式圓角分塊

很多 App 裡那種「上圓角 + 直角 + 下圓角」拼接的卡片列表,本質上就是拆成三個 shape:

圓角分塊效果
<!-- shape_test_top.xml:只有上面兩個角圓 -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:topLeftRadius="10dip"
        android:topRightRadius="10dip">
        <solid android:color="@android:color/white" />
        <stroke android:width="0.5dip" android:color="#44000000" />
    </corners>
</shape>
<!-- shape_test_middle.xml:四角都不圓 -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <stroke android:width="0.5dip" android:color="#44000000" />
</shape>
<!-- shape_test_bottom.xml:只有下面兩個角圓 -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:bottomLeftRadius="10dip"
        android:bottomRightRadius="10dip">
        <solid android:color="@android:color/white" />
        <stroke android:width="0.5dip" android:color="#44000000" />
    </corners>
</shape>

布局裡三個 View 依次引用這三個 shape,最後再加一個四角全圓的單獨卡片,視覺上就自然銜接了。

場景二:消息數量紅點

未讀消息的數字標記,用 shape 加 padding 就能做出膠囊效果:

<!-- shape_test_circle.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dip">
        <solid android:color="@android:color/holo_red_light">
            <padding
                android:left="3dip"
                android:top="1dip"
                android:right="3dip"
                android:bottom="1dip" />
        </solid>
    </corners>
</shape>

關鍵在於 padding:因為 radius 固定,只有靠左右內邊距撐開寬度,才能讓「1」和「99+」這種長度不同的文本都呈現均勻的膠囊形狀。這也是 shape 相比切圖的優勢——它能自適應內容變化。

小結

shape 標籤的記憶並不難:shape 定形狀,corners 定圓角,gradient 定漸變,solid 定純色,stroke 定邊框,padding 定內距,size 定尺寸。只需記住兩組互斥關係——solid 與 gradient 不能共存,innerRadius 與 innerRadiusRatio(thickness 同理)以前者為準——基本就能覆蓋日常開發中的絕大多數需求。

再回頭看開頭那份「註釋牆」,是不是清楚很多了?

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