android代码设置点击涟漪,android – 为自定义CompoundButton添加涟漪效果

我有以下自定义CompoundButton:

public class CustomCompoundButton extends CompoundButton {

public CustomCompoundButton(Context context) {

this(context, null);

}

public CustomCompoundButton(Context context, AttributeSet attrSet) {

super(context, attrSet);

setup();

}

private void setup() {

setBackgroundResource(R.drawable.button_selector);

setGravity(Gravity.CENTER);

setClickable(true);

}

}

在将布局添加到布局后,我从代码中设置了Button的宽度和高度:

button.getLayoutParams().width = myWidth;

button.getLayoutParams().height = myHeight;

button_selector.xml:

android:drawable="@drawable/button_checked"

android:state_checked="true" />

android:drawable="@drawable/button_unchecked"

android:state_checked="false" />

button_checked.xml:

android:shape="oval">

button_unchecked.xml:

android:shape="oval">

android:width="2dp"

android:color="?colorAccent" />

这按预期工作,未选中时按钮为空圆圈,选中时为实心圆圈.

问题是我无法在此行为之上添加涟漪效应.

我试图将选择器包装在ripple标签中,如下所示:

android:color="#ffffff">

android:drawable="@drawable/button_checked"

android:state_checked="true" />

android:drawable="@drawable/button_unchecked"

android:state_checked="false" />

这种方法存在多个问题:

>背景形状被涟漪完全覆盖,它们不再可见(无论它们是否被检查)

背景形状应该保持不变,我只想在点击按钮时添加涟漪效果(选中或取消选中)

>涟漪效应的半径太大,它们相互重叠

纹波半径应与我的按钮半径相同.

我不知道如何做这项工作,真的很感激任何建议.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值