如何进行径向反应? [关闭]
Posted
技术标签:
【中文标题】如何进行径向反应? [关闭]【英文标题】:How to make a radial reaction? [closed] 【发布时间】:2015-10-21 18:15:20 【问题描述】:Here,按键后在下方画了一个圆圈。如何实现这个?
【问题讨论】:
看看:- ***.com/a/24866786/1384010 看看这个***.com/questions/26604134/… 你试过什么? SO 真的不是为了让别人为你写代码。 @MaxvonHippel 我想找到任何现成的实现。这就是最好使用库的情况 在这种情况下,这个问题要求提供库或代码建议,这对于 SO 来说太宽泛了。 【参考方案1】:它被称为涟漪效应,它与材料设计一起引入。如果您还想支持棒棒糖之前的设备,您将需要两种不同的实现。为此,请打开您的 res 文件夹并创建两个额外的文件夹。命名第一个drawable(如果它不存在)和第二个drawable-v21(将由运行棒棒糖或更高版本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>
现在,在 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>
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>
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>
现在进入按钮存在的布局文件,使用以下行更新其代码:
<Button
....
android:background="@drawable/add_button_selector"
android:stateListAnimator="@null"
/>
按钮一开始是透明的,并且与您的背景颜色相匹配。 buttonColorPressed 将是深灰色,当您单击它时会出现。因此,您可以自己将其设置为最适合的那个。但是,在您的情况下,您我认为您只需要在透明背景上添加 opacity(alpha)。因此您可以尝试将 buttonColorPressed 设置为 #20000000 即:
替换:
android:color="@color/buttonColorPressed"
有
android:color="#20000000"
但是,在棒棒糖之前的设备中,上面的代码不会像棒棒糖设备中那样具有涟漪效果的动画感。在这种情况下,您可能需要在项目中包含以下库: https://github.com/balysv/material-ripple,帮你轻松整合动画效果。
【讨论】:
好像没有动画吧? @danpetruk 请看看我更新的答案。【参考方案2】:一个简单的方法是使用与您当前操作系统版本匹配的 ?attr 版本。
android:background="?attr/actionBarItemBackground"
or(当不在操作栏中时)
android:background="?selectableItemBackgroundBorderless"
【讨论】:
以上是关于如何进行径向反应? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章