天天看點

Android 按鈕實作按壓水波紋效果

方法一:

在控件中加入​

​android:foreground="?selectableItemBackground"​

​​ 即可實作水波紋的效果。

但是這需要在API23 也就是說需要在Android6.0系統的手機上面,才會有效果。

方法二:

給Button按鈕設定背景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RippleActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="水波紋效果"
        android:textSize="20sp"
        android:textColor="@color/white"
        />
</LinearLayout>      

在drawable檔案中設定ripple.xml

用水波紋标簽 設定水波紋的顔色,和Button按鈕的樣式:

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

</ripple>      

其中btn_ripple.xml屬性如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <solid android:color="#11EA09"/>
    <corners android:radius="25dp"/>
</shape>      

我水波紋按壓顔色設定的是白色,這樣能看的更直覺。