1. 程式人生 > >android 按鈕水波紋效果

android 按鈕水波紋效果

android 5.0以後。加入了觸控反饋動畫。當你使用了Material主題後,波紋動畫會自動應用在所有的控制元件上,我們當然可以來設定其屬性來調整到我們需要的效果。

按鈕波紋效果有很多種方法。介紹最常用的兩種

第一種:Material自帶水波紋

通過如下程式碼設定波紋的背景:

android:background="?android:attr/selectableItemBackground"波紋有邊界

android:background="?android:attr/selectableItemBackgroundBorderless"波紋超出邊界

為view新增波紋效果:
<RelativeLayout
                android:id="@+id/user_info_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:background="?android:attr/selectableItemBackground"
             >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:contentDescription="@null"
                    android:paddingRight="15dip"
                     />
            </RelativeLayout>
2.第二種方法 2.1 新增一個普通的 ripple_bg_drawable.xml 背景檔案
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#8cc476" />
    <corners android:radius="0dp" />
</shape>
2.2新增帶波紋效果的背景檔案 ripple_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#FF21272B">
    <item android:drawable="@drawable/ripple_bg_drawable" />
</ripple>