单击按钮时如何在按钮上产生圆形波纹? [英] How to make a circular ripple on a button when it's being clicked?
问题描述
在Android的拨号器应用程序上,当您开始搜索某些内容时,单击EditText左侧的箭头按钮,会在其上产生圆形波纹效果:
On the dialer app of Android, when you start searching for something, and you click the arrow button on the left of the EditText, you get a circular ripple effect on it :
我也尝试过拥有它,但是我得到了一个矩形的东西:
I've tried to have it too, but I got a rectangular one:
<ImageButton
android:id="@+id/navButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:background="?android:attr/selectableItemBackground"
android:src="@drawable/search_ic_back_arrow"/>
问题
如何在单击按钮时使按钮具有圆形波纹效果?我必须创建一个新的可绘制对象,还是有一种内置的方法?
The question
How do I make the button have a circular ripple effect when being clicked? Do I have to create a new drawable, or is there a built in way for that?
推荐答案
如果您已经有背景图片,那么下面是一个波纹示例,看上去很接近selectableItemBackgroundBorderless:
If you already have a background image, here is an example of a ripple that looks close to selectableItemBackgroundBorderless:
<ImageButton
android:id="@+id/btn_show_filter_dialog"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/ic_filter_state"/>
ic_filter_state.xml:
ic_filter_state.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/state_pressed_ripple"/>
<item
android:state_enabled="true"
android:drawable="@drawable/ic_filter" />
</selector>
state_pressed_ripple.xml :(在白色背景上将不透明度设置为10%)1AFFFFFF
state_pressed_ripple.xml: (opacity set to 10% on white background) 1AFFFFFF
<?xml version="1.0" encoding="UTF-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#1AFFFFFF"/>
</shape>
<color android:color="#FFF"/>
</item>
</ripple>
这篇关于单击按钮时如何在按钮上产生圆形波纹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!