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

Android 自訂背景:虛線、圓角與漸層一次搞定

編輯:Android 程式設計入門

先看效果,再談做法。

點擊效果:

一、設置虛線

在 res/drawable 下建立一個 XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke
        android:dashGap="3dp"
        android:dashWidth="6dp"
        android:width="1dp"
        android:color="#63a219" />
    <!-- 虛線的高度 -->
    <size android:height="1dp" />
</shape>

三個關鍵屬性:

  • dashWidth:每一段實線的寬度。
  • dashGap:實線之間空隙的寬度。
  • width:線條本身的粗細。

當 dashGap="0dp" 時,就退化成實線了。另外 android:shape="line" 時,size 的高度是必要的,否則可能完全不顯示。

二、設置圓角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充顏色 -->
    <solid android:color="#FFFFFF"></solid>
    <!-- 線的寬度與顏色 -->
    <stroke android:width="1dp" android:color="#63a219"></stroke>
    <!-- 矩形的圓角半徑 -->
    <corners android:radius="10dp" />
</shape>

最簡單的用法就是 <corners android:radius="10dp" />,四個角一致。

三、漸層填充 + 四角半徑分開設定

<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 分別對應左上、右上、左下、右下的圓角半徑 -->
    <corners
        android:topLeftRadius="0dp"
        android:topRightRadius="7dp"
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="7dp"/>
    <!-- 設置漸層 -->
    <gradient
        android:startColor="#9cff00"
        android:endColor="#197600"
        android:angle="270"/>
    <stroke
        android:width="1dp"
        android:color="#63a219" />
</shape>

關於 angle:它必須是 45 的倍數。0 是由左到右,90 是由下到上,270 是由上到下。這裡寫 270 就是常見的「上淺下深」效果。搞錯方向是新手最常踩的坑。

四、漸層 + 點擊效果

先做一個 style 讓版面引用比較乾淨:

<style name="list_item_top">
    <item name="android:clickable">true</item>
    <item name="android:focusable">true</item>
    <item name="android:paddingTop">10dip</item>
    <item name="android:paddingBottom">10dip</item>
    <item name="android:paddingLeft">10dip</item>
    <item name="android:paddingRight">10dip</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:background">@drawable/background_view_rounded_top</item>
</style>

接著用 selector 搭配 pressed 狀態,做出按下時換色的效果:

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="https://schemas.android.com/apk/res/android"
    android:insetLeft="1.0px"
    android:insetRight="1.0px" >

    <selector>
        <item android:state_pressed="true">
            <shape>
                <gradient
                    android:angle="270.0"
                    android:endColor="@color/base_end_color_pressed"
                    android:startColor="@color/base_start_color_pressed" />
                <corners
                    android:bottomLeftRadius="0.0dip"
                    android:bottomRightRadius="0.0dip"
                    android:radius="2.0dip"
                    android:topLeftRadius="10.0dip"
                    android:topRightRadius="10.0dip" />
                <stroke
                    android:width="1dp"
                    android:color="#eededede" />
            </shape>
        </item>
        <item>
            <shape>
                <gradient
                    android:angle="270.0"
                    android:endColor="@color/base_end_color_default"
                    android:startColor="@color/base_start_color_default" />
                <corners
                    android:bottomLeftRadius="0.0dip"
                    android:bottomRightRadius="0.0dip"
                    android:radius="2.0dip"
                    android:topLeftRadius="11.0dip"
                    android:topRightRadius="11.0dip" />
                <stroke
                    android:width="1dp"
                    android:color="#eededede" />
            </shape>
        </item>
    </selector>

</inset>

這裡有個容易忽略的小細節:兩個 item 的 topLeftRadius 一個是 10dip、一個是 11dip。這不是筆誤,而是為了補償 pressed 狀態下 stroke 視覺上的差異,避免按下瞬間圓角看起來「跳一下」。這類像素級的細節,通常要實機反覆比對才調得出來。

五、補充:4.0 以上虛線變實線的問題

這是留言區最熱的問題。Android 4.0 之後,硬體加速預設開啟,而虛線在硬體加速下會被繪製成實線。解法是對該 View 關閉硬體加速。

程式碼中加上:

line.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

或者在 XML 中加上:

android:layerType="software"

原因說明:早期的 PaintDrawable 在硬體加速管線下不支援 dash path effect,強行走硬體繪製就會退化成實線。指定 software layer 會讓這個 View 改用軟體繪製,虛線就回來了。代價是該 View 會佔用額外記憶體且效能略差,所以只對需要的 View 局部關閉即可,不要全域關掉硬體加速。

感謝留言區幾位朋友的參與與指正。

六、幾個實務補充

  • shape 也可以直接寫在 android:background="@drawable/xxx" 裡引用,不必額外寫 style。
  • 需要「只有底線、其他三邊沒有」時,用 <stroke> 是做不出來的,改用 <layer-list> 疊一層,或直接用 MaterialDivider。
  • Android 5.0 之後若使用 Material Design,圓角、漸層這類效果大多可以直接用 MaterialCardView 的 cardCornerRadius、cardElevation 屬性取代,程式碼更少也更好維護。
  • 漸層若想做成 sweep(扇形)或 radial(放射),XML 的 <gradient> 也支援 android:type 與 android:gradientRadius。

原始碼可於原文頁面下載。

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