如何为按钮添加带有渐变的波纹效果?

kav*_*vie 1 android gradient button ripple

我创建了一个按钮,我想为该按钮添加涟漪效果和渐变!

这是我的按钮代码:

<Button
    android:id="@+id/percentageButton"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    android:gravity="center"
    android:text="%"
    android:textColor="@android:color/white"
    android:textSize="30dp">
</Button>
Run Code Online (Sandbox Code Playgroud)

这是我的gradient.xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="0dp"/>
    <gradient
        android:gradientRadius="100"
        android:centerX="49%"
        android:centerY="50%"
        android:centerColor="#434343"
        android:startColor="#0F0F0F"
        android:endColor="#141414"
        android:type="radial"/>
  </shape>
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有的可能性,但没有得到如何实现这一点。

Der*_*rek 5

要在按下按钮时获得涟漪效果,只需将按钮 xml 更改为包括:

android:foreground="?attr/selectableItemBackground"

并在您的按钮上设置基本渐变,您可以:

 <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:angle="-90"
            android:startColor="COLORHERE"
            android:endColor="COLORHERE"
            android:type="linear" />
    </shape>
Run Code Online (Sandbox Code Playgroud)


Pei*_*Pei 5

android:foreground在 Lollipop 中对我不起作用,这是我的 xml 可绘制按钮。添加button_background.xml到您的绘图中。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorPrimaryDark">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimaryDark" />
            <corners android:radius="@dimen/button_radius_large" />
        </shape>
    </item>

    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <gradient
                android:angle="90"
                android:endColor="@color/colorPrimaryLight"
                android:startColor="@color/colorPrimary"
                android:type="linear" />
            <corners android:radius="@dimen/button_radius_large" />
        </shape>
    </item>
</ripple>
Run Code Online (Sandbox Code Playgroud)

将其添加到按钮的背景中。

<Button
    ...
    android:background="@drawable/button_background" />
Run Code Online (Sandbox Code Playgroud)

注意:属性android:foreground对低于23 的API级别没有影响