java ImageButton 中的透明背景具有涟漪效应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30364534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transparent background in ImageButton with ripple effect?
提问by Confuse
I want to programmaticallyremove the grey background in ImageButton. I tried many method of removing it like -
我想以编程方式删除 ImageButton 中的灰色背景。我尝试了很多删除它的方法,比如 -
imageButton.setBackgroundDrawable(null);
but on implementing them, I don't get the ripple effect on the ImageButton on touch. (No highlighting on touch).
但是在实施它们时,我没有在触摸时对 ImageButton 产生连锁反应。(触摸时没有突出显示)。
Is there any way to remove the background but preserve the ripple effect or highlight.
有什么方法可以去除背景但保留波纹效果或突出显示。
回答by Mohib Irshad
If android:background="?attr/selectableItemBackground"
this works than I believe this answer should solve your problem:
如果android:background="?attr/selectableItemBackground"
这有效,我相信这个答案应该可以解决您的问题:
回答by Malvin
Create your own RippleDrawable
and you need to use mask for the Ripple if you're going to use transparent background.
创建您自己的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>
回答by Tyg
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.
要有一个带涟漪效果的透明背景,背景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);
(Sorry for the JavaScript/NativeScript code, hope everyone can understand it)
(抱歉JavaScript/NativeScript 代码,希望大家能看懂)