Android Switch自定义实现IOS效果

先看下效果图在这里插入图片描述
自定义View

public class Seniorswitch extends Switch {
    /**
     * 监听是否存在动作
     */
    private boolean IF_EVENT_MOVE = false;
    /**
     * 监听滑动时间
     */
    private long MOVE_TIME = -1;

    public Seniorswitch(Context context) {
        super(context);
    }

    public Seniorswitch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Seniorswitch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (IF_EVENT_MOVE && ev.getAction() == MotionEvent.ACTION_UP) {
            MOVE_TIME = -1;
            IF_EVENT_MOVE = false;
            if (ev.getX() <= getWidth() / 2) {
                setChecked(false);
            } else {
                setChecked(true);
            }
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            //松开
            IF_EVENT_MOVE = false;
            MOVE_TIME = -1;
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            //动作
            if (MOVE_TIME == -1) {
                MOVE_TIME = System.currentTimeMillis();
            } else {
                if (System.currentTimeMillis() - MOVE_TIME > 500) {
                    IF_EVENT_MOVE = true;
                }
            }
        }
        return super.onTouchEvent(ev);
    }
}

再上switch_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true">
        <shape android:shape="oval">
            <!--背景色-->
            <solid android:color="#ffffff" />
            <!--边框-->
            <stroke android:width="@dimen/dp_2" android:color="#00000000" />
            <!--大小-->
            <size android:width="@dimen/dp17" android:height="@dimen/dp17" />
        </shape>
    </item>

    <item android:state_checked="false">
        <shape android:shape="oval">
            <!--背景色-->
            <solid android:color="#ffffff" />
            <!--边框-->
            <stroke android:width="@dimen/dp_2" android:color="#00000000" />
            <!--大小-->
            <size android:width="@dimen/dp17" android:height="@dimen/dp17" />
        </shape>
    </item>

</selector>

再上switch_track_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <solid android:color="#1E7AFA" />
            <!--圆角-->
            <corners android:radius="@dimen/dp9" />
            <!--大小-->
            <size android:width="@dimen/dp19" android:height="@dimen/dp19" />
        </shape>
    </item>

    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <solid android:color="#7E8187" />
            <!--圆角-->
            <corners android:radius="@dimen/dp9" />
            <!--大小-->
            <size android:width="@dimen/dp19" android:height="@dimen/dp19" />
        </shape>
    </item>
</selector>

xml布局中引用

<com.xx.xx.xx.xx.Seniorswitch
	android:id="@+id/switch_lock"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_alignParentRight="true"
	android:layout_centerVertical="true"
	android:layout_marginRight="17dp"
	android:background="#00000000"
	android:checked="false"
	android:thumb="@drawable/switch_selector"
	android:track="@drawable/switch_track_selector" />

Java代码中使用

switch_bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
        } else {
        }
    }
});

结束!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落魄的Android开发

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值