ImageButton中的透明背景具有涟漪效应?

时间:2015-05-21 04:21:25

标签: java android xml imagebutton

我想以编程方式删除ImageButton中的灰色背景。我尝试了许多删除它的方法,如 -

imageButton.setBackgroundDrawable(null);

但是在实施它们时,我不会在触摸时对ImageButton产生连锁反应。 (触摸时没有突出显示)。

有没有办法删除背景但保留涟漪效果或突出显示。

3 个答案:

答案 0 :(得分:36)

如果android:background="?attr/selectableItemBackground"这比我认为这个答案可以解决您的问题更有效:

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

答案 1 :(得分:9)

创建自己的RippleDrawable,如果要使用透明背景,则需要为Ripple使用遮罩。

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

答案 2 :(得分:0)

要具有带波纹效果的透明背景,背景Drawable必须是一个rippleDrawable,可以是透明的。像这样以编程方式进行设置。

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);

(对不起,JavaScript / NativeScript代码,希望每个人都能理解)