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

深入解析 Android 5.0 波紋(Ripple)效果的實現與自定義

編輯:關於 Android 程式設計

自從 Material Design 問世以來,波紋(Ripple)點擊反饋效果就成了大家耳熟能詳的互動體驗。用過 5.0 新控件的朋友對它應該不陌生,但如果換成 TextView、Button 這類普通控件,你知道該怎麼幫它們加上波紋效果嗎?本文就帶大家完整梳理波紋效果的兩條實現路徑:一是直接沿用系統內建屬性,二是自行定義 drawable 來客製化。

一、系統內建的波紋實現方式

1. 有界波紋(Bounded Ripple)

波紋效果大致可分為「有界」與「無界」兩種,我們先從有界波紋看起。所謂有界,指的是波紋的繪製範圍會被限制在控件自身所占據的區域之內。

效果如下:

關鍵代碼:

<textview
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:layout_height="56dp"
    android:layout_marginTop="36dp"
    android:layout_width="match_parent"
    android:text="Hello World!">
</textview>

寫法非常簡單,只要為 TextView 設置背景即可,背景內容採用系統自帶的 selectableItemBackground。這就是有界波紋,波紋會在 TextView 所在的區域內進行繪製。

2. 無界波紋(Borderless Ripple)

關鍵代碼:

<textview
    android:background="?android:attr/selectableItemBackgroundBorderless"
    android:clickable="true"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:layout_height="56dp"
    android:layout_marginTop="36dp"
    android:layout_width="match_parent"
    android:text="Hello World!">
</textview>

這裡所說的「無界」並非完全沒有邊界,而是指以控件寬、高中較大的那個數值作為邊長,畫出一個正方形範圍,波紋就在這個正方形內擴散。以上就是系統自帶的兩種波紋效果,那如果想自己動手定義又該如何是好呢?

二、自定義波紋的實現方式

1. 無界波紋

自定義其實並不難,只要在 drawable 文件夾中定義一個 ripple 節點,並指定顏色即可:

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/colorAccent"
    xmlns:android="https://schemas.android.com/apk/res/android">
</ripple>

接著在佈局文件中將它引用為控件的背景:

<textview
    android:background="@drawable/nomaskripple"
    android:clickable="true"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:layout_height="56dp"
    android:layout_marginTop="36dp"
    android:layout_width="match_parent"
    android:text="Hello World!">
</textview>

呈現效果如下:

這就是無界波紋。那如果想要定義有界波紋呢?

2. 有界波紋

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/colorPrimary"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/mask"
        android:drawable="@color/colorAccent">
    </item>
</ripple>

有界波紋需要我們在 ripple 節點中定義一個 item,其 id 必須為系統提供的 mask,同時還要指定 drawable。值得注意的是,這裡 drawable 的顏色其實並不影響最終呈現,波紋的顏色是由 ripple 節點上的 color 屬性來決定的。效果如下:

3. 帶圖片形狀的波紋

有時候你可能不希望波紋是長條形,這時有兩種解法:一是使用圖片,二是自定義 shape。先來看圖片的做法:

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/colorAccent"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/mask"
        android:drawable="@drawable/ic_launcher">
    </item>
</ripple>

這裡我用了系統自帶的小機器人圖示,效果如下:

可以看到,此時波紋的範圍就是這張圖片中非透明像素所覆蓋的區域。

4. 自繪形狀的波紋

接著看自繪 shape 的做法,這裡以一個圓角矩形為例:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <corners android:radius="10dp">
        <solid android:color="@color/colorPrimary">
        </solid>
    </corners>
</shape>

再在 ripple 中引用這個矩形:

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/colorAccent"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/mask"
        android:drawable="@drawable/custom_shape">
    </item>
</ripple>

顯示效果:

這種方式下,shape 中定義的顏色只是用來框定波紋的顯示範圍,對視圖本身的顯示並沒有作用。如果你希望控件一開始就呈現 shape 中定義的底色,可以改成下面這樣定義 ripple:

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/colorAccent"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="10dp">
                <solid android:color="@color/colorPrimary">
                </solid>
            </corners>
        </shape>
    </item>
</ripple>

效果如下:

既然 item 中可以定義 shape,那麼有沒有辦法在 item 裡定義 selector 呢?當然可以。

5. 帶 selector 效果的波紋

代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/colorAccent"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <item>
        <selector>
            <item android:drawable="@drawable/ic_launcher"
                android:state_pressed="true">
            <item android:drawable="@drawable/bg"
                android:state_pressed="false">
            </item>
        </selector>
    </item>
</ripple>

顯示效果:

以上就是 Android 5.0 中波紋效果的完整用法。掌握系統屬性與自定義 drawable 這兩套思路,就能在各種控件上靈活地加上 Material Design 風格的點擊反饋了。

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