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

Android Shape 詳解:用 XML 輕鬆定義矩形、圓形、漸變與描邊背景

編輯:關於 Android 程式設計

說白了,shape 就是用來在 XML 文件裡定義各種形狀的一種手段,系統解析之後,它就能像普通 Drawable 一樣被直接使用。掌握它,能讓你在不寫一行繪製代碼的前提下,快速做出漂亮的按鈕與卡片背景。

官方說明

本質上,shape 定義的是一種 Drawable。它的文件放在 res/drawable/filename.xml 中,編譯後的資源類型是 GradientDrawable。

引用方式:在 Java 中寫 R.drawable.filename;在 XML 中寫 @[package:]drawable/filename。

其基本語法結構如下:

 <shape xmlns:android="https://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>

這裡僅做語法概覽,重點還是在下面的實際用法。各元素的細節可查閱官方 shape 文檔。

實際使用

矩形

填充(solid)——最簡單的一種,只給形狀鋪上一種純色:

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


描邊(stroke)——在填充色的基礎上再勾勒一條邊框:

 <shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorAccent" />
    <stroke
        android:width="3dp"
        android:color="@color/colorPrimaryDark"
        android:dashGap="0dp"
        android:dashWidth="10dp" />
</shape>

如果把 dashGap 設為非 0 值(例如下圖的 4dp),邊框就會變成虛線;再配合 corners 還能做出圓角效果:

 <shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorAccent" />
    <stroke
        android:width="3dp"
        android:color="@color/colorPrimaryDark"
        android:dashGap="4dp"
        android:dashWidth="10dp" />
    <corners
        android:bottomLeftRadius="60dp"
        android:radius="30dp"
        android:topRightRadius="120dp" />
</shape>


漸變(gradient):linear 線性漸變,沿指定角度平滑過渡:

 <shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="3dp"
        android:color="@color/colorPrimaryDark"
        android:dashGap="4dp"
        android:dashWidth="10dp" />
    <corners
        android:bottomLeftRadius="60dp"
        android:radius="30dp"
        android:topRightRadius="120dp" />
    <gradient
        android:angle="45"
        android:centerColor="@color/stone"
        android:endColor="@color/pink"
        android:startColor="@color/yellow" />
</shape>

漸變(gradient):radial 徑向漸變,以圓心為中心向外擴散,此時需要指定 gradientRadius:

 <gradient
    android:angle="90"
    android:startColor="@color/colorPrimary"
    android:centerColor="@color/pink"
    android:endColor="@color/yellow"
    android:gradientRadius="400dp"
    android:type="radial"/>

漸變(gradient):sweep 掃描漸變,顏色繞中心呈扇形旋轉分佈:

 <gradient
    android:startColor="@color/colorPrimary"
    android:centerColor="@color/pink"
    android:endColor="@color/yellow"
    android:gradientRadius="400dp"
    android:type="sweep"/>


圓形

正圓——只要把 shape 設為 oval,並讓 size 的寬高相等即可:

 <shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="oval">
    <gradient
        android:centerColor="@color/pink"
        android:endColor="@color/yellow"
        android:startColor="@color/colorPrimary" />
    <size
        android:width="400dp"
        android:height="400dp" />
</shape>


橢圓——不指定 size,讓它自適應容器比例,就會被拉成橢圓:

 <shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="oval">
    <gradient
        android:angle="90"
        android:centerColor="@color/pink"
        android:endColor="@color/yellow"
        android:startColor="@color/colorPrimary" />
</shape>


線條

 <shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="9dp"
        android:color="@color/pink" />
</shape>


環形

 <shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadius="100dp"
    android:thickness="50dp"
    android:useLevel="false" >
    <gradient
        android:startColor="@color/colorAccent"
        android:endColor="@color/yellow"
        android:centerColor="@color/pink"/>
</shape>

特別提醒:android:useLevel 一定要設成 false,否則環形很可能根本不會顯示出來。依據 Google 官方的解釋:

以下幾個屬性只在 android:shape="ring" 時才會生效:

android:innerRadius —— 內環的半徑。

android:thickness —— 環的厚度。

android:innerRadiusRatio —— 浮點型,以環寬的比率來表示內環半徑。例如設為 5,則內環半徑等於環寬除以 5;它會被 android:innerRadius 覆蓋,默認值為 9。

android:thicknessRatio —— 浮點型,以環寬的比率來表示環的厚度。例如設為 2,環厚即為環寬除以 2;它會被 android:thickness 覆蓋,默認值為 3。

android:useLevel —— 布爾值,若作為 LevelListDrawable 使用則為 true,否則為 false。通常設為 false。


小結

這些以 shape 為根節點的 Drawable XML 文件,既可以當作 Button、TextView 等視圖的背景,也能借助 size 屬性做成簡潔的小圖標。可以說,每一個不起眼的屬性背後都藏著可挖掘的價值。還是那句老話:多翻官方文檔,總有意想不到的收穫。

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