如何做出径向反应?

kan*_*ndi 0 android android-animation material-design

在这里,按钮触摸后,它下面画一个圆圈.如何实现这个?

在此输入图像描述

Ale*_*sas 9

它被称为涟漪效应,它与材料设计一起被引入.如果你想支持和棒棒糖前设备,你需要两个不同的实现.为此,请打开res文件夹并创建另外两个文件夹.将第一个drawable(如果它尚未存在)和第二个drawable-v21命名(将由运行lollipop或更高版本API的设备使用).在drawable-v21文件夹中,创建一个名为add_button_selector.xml的文件,并添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/buttonColorPressed">
    <item>
        <shape android:shape="oval">
            <solid android:color="#00000000"/> 
        </shape>
    </item>
</ripple>
Run Code Online (Sandbox Code Playgroud)

现在,在drawable文件夹中添加以下三个XML文件:

add_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#00000000"/> 
</shape>
Run Code Online (Sandbox Code Playgroud)

add_button_selected.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/buttonColorPressed"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

add_button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/add_button_selected"/>
    <item android:state_pressed="true" android:drawable="@drawable/add_button_selected"/>
    <item android:drawable="@drawable/add_button"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

现在进入按钮所在的布局文件,使用以下行更新其代码:

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

该按钮在开始时将是透明的,它将与您的背景颜色相匹配.buttonColorPressed它将是更深的灰色,当你点击它时会出现.因此,您可以将其自己设置为最适合的那个.但是,在您的情况下,我认为您只需要在透明背景上添加不透明度(alpha).因此,您可以尝试将buttonColorPressed设置为#20000000,即:

更换:

android:color="@color/buttonColorPressed"
Run Code Online (Sandbox Code Playgroud)

android:color="#20000000"
Run Code Online (Sandbox Code Playgroud)

但是,前棒棒糖设备中的上述代码不会像棒棒糖设备那样具有动画效果.在这种情况下,您可能需要在项目中包含以下库:https: //github.com/balysv/material-ripple,它将帮助您轻松集成动画效果.