用Canvas画Switch控件

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class SwitchView extends View implements View.OnTouchListener {
    private int downx, downy;
    private boolean isfling = false;
    private float width = 0;
    private float height = 0;
    private float radius = 0;
    private boolean state = false;
    private boolean initstate = false;
    private onChangeListener listener;
    private boolean touch = false;

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

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setOnTouchListener(this);
        
        if (downx < 0 ){
            state = false;
            checkedBg(canvas, "#cfcfcf","#c1c1c1");
            slipperBtn(canvas, radius, radius, radius - 4);
        }else if(downx > width){
            state = true;
            checkedBg(canvas, "#00aaef","#0098d5"); //checked
            slipperBtn(canvas, width - radius, radius, radius - 4);
        }else{
            if (downx > width / 2) {
                if (isfling) {
                    state = true;
                    checkedBg(canvas, "#00aaef","#0098d5"); //checked
                    slipperBtn(canvas, downx, radius, radius - 4);
                } else {
                    state = true;
                    checkedBg(canvas, "#00aaef","#0098d5"); //checked
                    slipperBtn(canvas, width - radius, radius, radius - 4);
                }
            } else {
                if (isfling) {
                    state = false;
                    checkedBg(canvas, "#cfcfcf","#c1c1c1"); //unchecked
                    slipperBtn(canvas, downx, radius, radius - 4);
                } else {
                    state = false;
                    checkedBg(canvas, "#cfcfcf","#c1c1c1");
                    slipperBtn(canvas, radius, radius, radius - 4);
                }
            }
        }
        if (listener != null) {
            listener.OnChanged(this, state,touch);
        }
    }

    public void setWidth(int width) {
        this.width = width * 3 / 17f;
        this.height = this.width * 2 / 5f;
        this.radius = this.height / 2f;
    }

    public void checkedBg(Canvas canvas, String color, String outsidecolor) {
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setStyle(Paint.Style.FILL);
        p.setColor(Color.parseColor(color));

        RectF rec = new RectF(0, 0, width, height);
        canvas.drawRoundRect(rec, radius, radius, p);


        Paint po = new Paint();
        po.setAntiAlias(true);
        po.setStyle(Paint.Style.STROKE);
        po.setColor(Color.parseColor(outsidecolor));

        RectF reco = new RectF(0, 0, width, height);
        canvas.drawRoundRect(reco, radius, radius, po);

    }

    public void slipperBtn(Canvas canvas, float cx, float cy, float radius) {
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setStyle(Paint.Style.FILL);
        p.setColor(Color.parseColor("#ffffff"));

        canvas.drawCircle(cx, cy, radius, p);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        downx = (int) event.getX();
        downy = (int) event.getY();
        touch = true;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isfling = true;
                break;
            case MotionEvent.ACTION_MOVE:
                isfling = true;
                break;
            case MotionEvent.ACTION_UP:
                isfling = false;
                break;
        }
        invalidate();
        return true;
    }

    public void setOnChanged(onChangeListener changelistener) {
        this.listener = changelistener;
    }

    public interface onChangeListener {
        public void OnChanged(SwitchView switchView, boolean checkState,boolean touched);
    }

    public void setInitChanges(boolean changes){
        if(changes){
            downx = (int)width;
        }else{
            downx = 0;
        }
        isfling = false;
        invalidate();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值