Implementing Ripple effect outside ImageButton

微笑、不失礼 提交于 2019-12-30 01:37:09

问题


I am trying to implement ripple effect for ImageButton.I have set ripple in background and drawable image in the src for it.

android:background="@drawable/myripplexml"
android:src="@drawable/myimagepath"

Its giving nice ripple effect inside button Layout. But I want Ripple effect to extend outside the Button Layout also.Another way to do it is using :

 android:background="?android:attr/selectableItemBackgroundBorderless"

But it uses default color and style. How can I customize it regarding color, shape and it's size ?


回答1:


I ran into this and my issue is that 'selectableItemBackgroundBorderless' creates a rectangle, while my button was circular. I'm not sure if this answers the original question but here is what I found: set the background to this in drawable-v21

<ripple
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:colorControlHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

and @null in lower api levels (assuming you're using a selector for the actual image button src). The ripple most visible in the padding of the button. If there's no masking layer at all The ripple is unbound and kind of takes over the whole screen. You can use whatever shape you want if not a circle.




回答2:


If you are lazy like me, make use of the default ripple and forget about backward compatibility. Basically there are two types of background ripple u can add to ur views:

 ?android:selectableItemBackground // Ripple within view
 ?android:selectableItemBackgroundBorderless //Ripple outside view

Unfortunately,these ripples are always grey in color. To tweak these based on your theme, go to ur parent theme and change the colorControlHighlight.

In your app's main theme:

<item name="colorControlHighlight">#1bff3a</item>

This value will change the colour of the default ripple.

Note: On devices below 21, the ripple will turn into notmal selectors with the same color you have set.




回答3:


Just like @alan mentioned in his comment. If you create a ripple item with no mask and child items and add it as a views background, the ripple will be drawn on top of the first available parent and may extend the views bounds. This is mentioned in the doc

<!-- An unbounded red ripple. --/>
<ripple android:color="#ffff0000" />


来源:https://stackoverflow.com/questions/27393648/implementing-ripple-effect-outside-imagebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!