Transparent background in ImageButton with ripple effect?

六眼飞鱼酱① 提交于 2019-11-30 01:48:17
Mohib Irshad

If android:background="?attr/selectableItemBackground" this works than I believe this answer should solve your problem:

https://stackoverflow.com/a/28087443/2534007

Create your own RippleDrawable and you need to use mask for the Ripple if you're going to use transparent background.

 <!-- A red ripple masked against an opaque rectangle. --/>
 <ripple android:color="#ffff0000">
   <item android:id="@android:id/mask"
         android:drawable="@android:color/white" />
 </ripple>

To have a transparent background with ripple effect, the background Drawable needs to be a rippleDrawable, which can be transparent. Set it up programmatically like this.

var mask = new android.graphics.drawable.GradientDrawable();
mask.setShape(android.graphics.drawable.GradientDrawable.RECTANGLE);
mask.setColor(0xFF000000); // the color is irrelevant here, only the alpha
mask.setCornerRadius(5); // you can have a rounded corner for the effect

var rippleColorLst = android.content.res.ColorStateList.valueOf(
    android.graphics.Color.argb(255,50,150,255) // set the color of the ripple effect
);

// aaaand
var ripple = new android.graphics.drawable.RippleDrawable(rippleColorLst,null,mask);
yourImageButton.setBackground(ripple);

(Sorry for the JavaScript/NativeScript code, hope everyone can understand it)

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