android – 为自定义CompoundButton添加涟漪效果

android – 为自定义CompoundButton添加涟漪效果,第1张

概述我有以下自定义CompoundButton: public class CustomCompoundButton extends CompoundButton { public CustomCompoundButton(Context context) { this(context, null); } public CustomCompoundButto 我有以下自定义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:

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item        androID:drawable="@drawable/button_checked"        androID:state_checked="true" />    <item        androID:drawable="@drawable/button_unchecked"        androID:state_checked="false" /></selector>

button_checked.xml:

<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:shape="oval">    <solID androID:color="?colorAccent" /></shape>

button_unchecked.xml:

<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:shape="oval">    <stroke        androID:wIDth="2dp"        androID:color="?colorAccent" /></shape>

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

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

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

<?xml version="1.0" enCoding="utf-8"?><ripple xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:color="#ffffff">    <selector>        <item            androID:drawable="@drawable/button_checked"            androID:state_checked="true" />        <item            androID:drawable="@drawable/button_unchecked"            androID:state_checked="false" />    </selector></ripple>

这种方法存在多个问题:

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

背景形状应该保持不变,我只想在点击按钮时添加涟漪效果(选中或取消选中)
>涟漪效应的半径太大,它们相互重叠

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

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

@H_403_59@解决方法 将每个形状包裹在其自身的纹波中而不是将整个选择器包裹在波纹中将具有期望的效果.

有关示例,请参见this comment.

所以,button_unchecked.xml看起来像:

<?xml version="1.0" enCoding="utf-8"?><ripple xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:color="#ffffff"    tools:targetAPI="lollipop">    <item androID:ID="@androID:ID/mask">        <shape androID:shape="oval">            <stroke                androID:wIDth="2dp"                androID:color="?colorAccent" />        </shape>    </item>    <item        androID:ID="@androID:ID/background"        androID:drawable="?colorAccent" /></ripple>
总结

以上是内存溢出为你收集整理的android – 为自定义CompoundButton添加涟漪效果全部内容,希望文章能够帮你解决android – 为自定义CompoundButton添加涟漪效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1126056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存