繁体   English   中英

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

[英]Transparent background in ImageButton with ripple effect?

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

imageButton.setBackgroundDrawable(null);

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

有什么方法可以去除背景但保留波纹效果或突出显示。

如果android:background="?attr/selectableItemBackground"这比我相信这个答案应该解决你的问题有效:

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

创建您自己的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>

要有一个带涟漪效果的透明背景,背景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 代码,希望大家能看懂)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM