Android 自定义按钮

此篇博客介绍了如何创建一个名为MyToggleButton的自定义视图组件,它支持点击和滑动切换功能。通过继承View并重写相关方法,实现了一个可以动态调整背景和滑块位置的按钮,适用于Android开发中需要高度定制交互的场景。
摘要由CSDN通过智能技术生成

MyToggleButton

自定义按钮:支持点击和滑动切换

在这里插入图片描述

public class MyToggleButton extends View implements View.OnClickListener {
    private static int MAX_SLIDE_INSTANCE;
    private static final boolean DEFAULT_OPEN = false;

    private Paint paint;
    private Bitmap backgrounBitmap;
    private Bitmap slideBitmap;
    private boolean isOpen = DEFAULT_OPEN;
    private int slideInstance = 0;
    private boolean isClickEnable = false;

    public MyToggleButton(Context context) {
        this(context, null);
    }

    public MyToggleButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyToggleButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgrounBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.switch_background);
        slideBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.slide_button);
        MAX_SLIDE_INSTANCE = backgrounBitmap.getWidth() - slideBitmap.getWidth();
        if (isOpen) {
            slideInstance = MAX_SLIDE_INSTANCE;
        } else {
            slideInstance = 0;
        }
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (isClickEnable) {
            isOpen = !isOpen;
            refreshView();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(backgrounBitmap.getWidth(), backgrounBitmap.getHeight());
    }

    private int startX;
    private int lastX;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        int eventX = (int) event.getX();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = lastX = eventX;
                isClickEnable = true;
                break;
            case MotionEvent.ACTION_MOVE:
                float offsetX = eventX - lastX;
                slideInstance += offsetX;
                if (slideInstance < 0) {
                    slideInstance = 0;
                } else if (slideInstance > MAX_SLIDE_INSTANCE) {
                    slideInstance = MAX_SLIDE_INSTANCE;
                }
                invalidate();
                lastX = eventX;

                //如果滑动了,则不响应点击事件
                if (Math.abs(lastX - startX) > 5) {
                    isClickEnable = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (!isClickEnable) {
                    if (slideInstance > MAX_SLIDE_INSTANCE / 2) {
                        isOpen = true;
                    } else {
                        isOpen = false;
                    }
                    refreshView();
                }
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(backgrounBitmap, 0, 0, paint);
        canvas.drawBitmap(slideBitmap, slideInstance, 0, paint);
    }

    private void refreshView() {
        if (isOpen) {
            slideInstance = MAX_SLIDE_INSTANCE;
        } else {
            slideInstance = 0;
        }
        invalidate();
    }
}

代码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值