1.多组合按钮(不能拖动,只能点击)

public class TwoButtonSelectView extends View {

    private Paint textPaint;

    private boolean isselect = true;

    private Paint bgPaint;

    private boolean isClickable;

    private boolean isMove;

    private onButtonSelectListener onButtonSelectListener;

    private float cy, cx1, cx2;

    private String text1 = "";

    private String text2 = "";

    private float textSize;

    private int selectid = 1;

    private float width, height;

    private int mbgColor;
    private int mbgSelectColor;

    private Context context;

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

    public TwoButtonSelectView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TwoButtonSelectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData(context, attrs);
        initView();
    }

    private void initData(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TwoSlideSelectButton);
        text1 = a.getString(R.styleable.TwoSlideSelectButton_text1);
        text2 = a.getString(R.styleable.TwoSlideSelectButton_text2);

        textSize = a.getDimension(R.styleable.TwoSlideSelectButton_textSize, 35);

        mbgColor = a.getColor(R.styleable.TwoSlideSelectButton_backgroundcolor, context.getColor(R.color.niu_noselect));
        mbgSelectColor = a.getColor(R.styleable.TwoSlideSelectButton_selectbackgroundcolor, context.getColor(R.color.niu_select_blue));

        this.context = context;

        a.recycle();
    }

    private void initView() {
        textPaint = new Paint();
        textPaint.setTextSize(textSize);
        textPaint.setAntiAlias(true);

        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);

    }

    public void setSelectid(int selectid) {
        this.selectid = selectid;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureWidth(widthMeasureSpec);
        height = measureHeight(heightMeasureSpec);

        setMeasuredDimension((int) width, (int) height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        cx1 = width / 4 - (textPaint.measureText(text1) / 2);
        Log.d("OOO", "width=" + width + "--height=" + height);

        cx2 = (width * 3) / 4 - (textPaint.measureText(text2) / 2);

        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.bottom - fontMetrics.top;
        cy = height - (height - fontHeight) / 2 - fontMetrics.bottom;

        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setColor(mbgColor);
        RectF oval = new RectF(0, 0, width, height);
        canvas.drawRoundRect(oval, height/2, height/2, bgPaint);

        switch (selectid) {
            case 0:
                textPaint.setColor(context.getColor(R.color.niu_noselect_text));
                canvas.drawText(text1, cx1, cy, textPaint);
                canvas.drawText(text2, cx2, cy, textPaint);
                break;
            case 1:
                bgPaint.setColor(context.getColor(R.color.niu_select_blue));
                RectF oval1 = new RectF(0, 0, width / 2, height);
                canvas.drawRoundRect(oval1, height/2, height/2, bgPaint);

                textPaint.setColor(Color.WHITE);
                canvas.drawText(text1, cx1, cy, textPaint);
                textPaint.setColor(context.getColor(R.color.niu_noselect_text));
                canvas.drawText(text2, cx2, cy, textPaint);
                break;
            case 2:
                bgPaint.setColor(context.getColor(R.color.niu_select_blue));
                RectF oval2 = new RectF((width / 2), 0, width, height);
                canvas.drawRoundRect(oval2, height/2, height/2, bgPaint);

                textPaint.setColor(context.getColor(R.color.niu_noselect_text));
                canvas.drawText(text1, cx1, cy, textPaint);
                textPaint.setColor(Color.WHITE);
                canvas.drawText(text2, cx2, cy, textPaint);
                break;
            default:
                break;
        }

    }

    private void PerformSelect(int select) {
        if (onButtonSelectListener != null) {
            onButtonSelectListener.onSelect(select);
        }
        switch (select) {
            case 1:
                selectid = 1;
                break;
            case 2:
                selectid = 2;
                break;
            default:
                break;

        }
    }

    float startX, lastX, disX, startY, lastY, disY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isClickable = true;
                startX = event.getX();
                startY = event.getY();
                isMove = false;
                break;
            case MotionEvent.ACTION_MOVE:
                lastX = event.getX();
                lastY = event.getY();
                disX = lastX - startX;
                disY = lastY - startY;
                if (Math.abs(disX) < 5) break;
                isMove = true;
                isClickable = false;
                startX = event.getX();
                startY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                if (isselect) {
                    if (isClickable) {
                        if (event.getY() < height) {
                            if (event.getX() < width / 2 && event.getX() > 0) {
                                PerformSelect(1);
                            } else if (event.getX() < width && event.getX() > (width / 2)) {
                                PerformSelect(2);
                            }
                            invalidate();
                        }
                    }
                    if (isMove) {
                        if (!(Math.abs(disX) > Math.abs(disY))) {
                            return false;
                        }
                        if (disX > 0) {
                            if (selectid < 2) {
                                selectid++;
                                PerformSelect(selectid);
                            }
                        } else {
                            if (selectid > 1) {
                                selectid--;
                                PerformSelect(selectid);
                            }
                        }
                        invalidate();
                    }
                }
                break;
        }
        return true;
    }

    public interface onButtonSelectListener {
        void onSelect(int buttonid);
    }

    public TwoButtonSelectView.onButtonSelectListener getOnButtonSelectListener() {
        return onButtonSelectListener;
    }

    public void setOnButtonSelectListener(TwoButtonSelectView.onButtonSelectListener onButtonSelectListener) {
        this.onButtonSelectListener = onButtonSelectListener;
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = 1080;
        if (specMode == MeasureSpec.AT_MOST) {
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = 120;
        if (specMode == MeasureSpec.AT_MOST) {
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }
    public void setSelected(boolean isselected,int id){
        isselect = isselected;
        selectid = id;
        invalidate();
    }
    public int getSelected(){
        return selectid;
    }

2.能拖动:(希望能强制按钮变动位置,而不是用户手动操作)

private static String TAG = "ThreeLaneDepartureWarningLaneKeepingSelect";
private TextView tv1, tv2, tv3;
private Button button1, button2, button3;
private int dx, dy, l, b, r, t;
private ThreeLaneDepartureWarningLaneKeepingSelect.onClickButtonListener onClickButtonListener;
private int selectid = 1;
public ThreeLaneDepartureWarningLaneKeepingSelect(Context context) {
    super(context,null);
}
public ThreeLaneDepartureWarningLaneKeepingSelect(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.lane_departure_warning_lane_keeping_select, this);
    tv1 = findViewById(R.id.tv1);
    tv2 = findViewById(R.id.tv2);
    tv3 = findViewById(R.id.tv3);
    button1 = findViewById(R.id.button1);
    button1.setText(getResources().getString(R.string.lane_keeping_off));
    button1.setTextColor(Color.parseColor("#FFFFFFFF"));
    button1.setBackgroundResource(R.drawable.shape_bu_bg);
    button1.setOnTouchListener(shopCarSettleTouch);

    button2 = findViewById(R.id.button2);
    button2.setText(getResources().getString(R.string.lane_ldw));
    button2.setTextColor(Color.parseColor("#FFFFFFFF"));
    button2.setBackgroundResource(R.drawable.shape_bu_bg);
    button2.setOnTouchListener(shopCarSettleTouch);

    button3 = findViewById(R.id.button3);
    button3.setText(getResources().getString(R.string.ldw_and_lka));
    button3.setTextColor(Color.parseColor("#FFFFFFFF"));
    button3.setBackgroundResource(R.drawable.shape_bu_bg);
    button3.setOnTouchListener(shopCarSettleTouch);

    tv1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setVisibility(View.VISIBLE);
            button2.setVisibility(View.GONE);
            button3.setVisibility(View.GONE);
             onClickButtonListener.onClickButton1();
        }
    });
    tv2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setVisibility(View.GONE);
            button3.setVisibility(View.GONE);
            button2.setVisibility(View.VISIBLE);
             onClickButtonListener.onClickButton2();
        }
    });
    tv3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setVisibility(View.GONE);
            button2.setVisibility(View.GONE);
            button3.setVisibility(View.VISIBLE);
             onClickButtonListener.onClickButton3();
        }
    });
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setVisibility(View.VISIBLE);
            button2.setVisibility(View.GONE);
            button3.setVisibility(View.GONE);
            onClickButtonListener.onClickButton1();
        }
    });
    button2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setVisibility(View.GONE);
            button3.setVisibility(View.GONE);
            button2.setVisibility(View.VISIBLE);
            onClickButtonListener.onClickButton2();
        }
    });
    button3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setVisibility(View.GONE);
            button2.setVisibility(View.GONE);
            button3.setVisibility(View.VISIBLE);
            onClickButtonListener.onClickButton3();
        }
    });
}
public void setSelectid(int selectid) {
    this.selectid = selectid;
    switch (selectid){
        case 1:
            button1.setVisibility(View.VISIBLE);
            button2.setVisibility(View.GONE);
            button3.setVisibility(View.GONE);
            break;
        case 2:
            button2.setVisibility(View.VISIBLE);
            button1.setVisibility(View.GONE);
            button3.setVisibility(View.GONE);
            break;
        case 3:
            button3.setVisibility(View.VISIBLE);
            button1.setVisibility(View.GONE);
            button2.setVisibility(View.GONE);
            break;
        default:
            break;
    }
    invalidate();
}

private OnTouchListener shopCarSettleTouch = new OnTouchListener() {
    int lastX, lastY;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int ea = event.getAction();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int screenWidth = 668;
        Log.i(TAG, "screenWidth: " + screenWidth);
        int screenHeight = dm.heightPixels;
        switch (ea) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "onTouch: " + "ACTION_DOWN");
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "onTouch: " + "ACTION_MOVE");
                dx = (int) event.getRawX() - lastX;
                dy = (int) event.getRawY() - lastY;
                l = v.getLeft() + dx;
                b = v.getBottom();
                r = v.getRight() + dx;
                t = v.getTop();
                if (l < 0) {
                    l = 0;
                    r = l + v.getWidth();
                }
                if (t < 0) {
                    t = 0;
                    b = t + v.getHeight();
                }
                if (r > screenWidth) {
                    r = screenWidth;
                    l = r - v.getWidth();
                }
                if (b > screenHeight) {
                    b = screenHeight;
                    t = b - v.getHeight();
                }
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                v.layout(l, t, r, b);
                v.postInvalidate();
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "onTouch: " + "ACTION_UP");
                if ((l >= 0 && l < (screenWidth/6))&&(r >= (screenWidth/3) && r < (screenWidth/2))) {
                    button1.setVisibility(View.VISIBLE);
                    button2.setVisibility(View.GONE);
                    button3.setVisibility(View.GONE);
                    button1.layout(l = 0, t, r = (screenWidth/3), b);
                    onClickButtonListener.onClickButton1();
                } else if ((l >= (screenWidth/6) && l < (screenWidth/2))&&(r >= (screenWidth/2) && r < (5*screenWidth/6))) {
                    button1.setVisibility(View.GONE);
                    button2.setVisibility(View.VISIBLE);
                    button3.setVisibility(View.GONE);
                    button2.layout(l = (screenWidth/3), t, r = (2*screenWidth/3), b);
                    onClickButtonListener.onClickButton2();
                } else if (l >= (screenWidth/2) && r >= (5*screenWidth/6)) {
                    button1.setVisibility(View.GONE);
                    button2.setVisibility(View.GONE);
                    button3.setVisibility(View.VISIBLE);
                    button3.layout(l = (2*screenWidth/3), t, r = screenWidth, b);
                    onClickButtonListener.onClickButton3();
                }
                v.layout(l, t, r, b);
                v.postInvalidate();
                Log.e(TAG, "ACTION_UP: " + l + "==" + t + "==" + r + "==" + b);
                break;
        }
        return true;
    }
};
public interface onClickButtonListener {
    void onClickButton1();
    void onClickButton2();
    void onClickButton3();
}

public ThreeLaneDepartureWarningLaneKeepingSelect.onClickButtonListener getOnClickButtonListener() {
    return onClickButtonListener;
}

public void setOnClickButtonListener(ThreeLaneDepartureWarningLaneKeepingSelect.onClickButtonListener onClickButtonListener) {
    this.onClickButtonListener = onClickButtonListener;
}

attrs.xml中:

<resources>
    <attr name="text1" format="string" />
    <attr name="text2" format="string" />
    <attr name="text3" format="string" />
    <attr name="text4" format="string" />
    <attr name="text5" format="string" />
    <attr name="text6" format="string" />
    <attr name="textSize" format="dimension" />
    <attr name="backgroundcolor" format="dimension" />
    <attr name="selectbackgroundcolor" format="dimension" />
    <declare-styleable name="TwoSlideSelectButton">
        <attr name="text1" />
        <attr name="text2" />
        <attr name="textSize" />
        <attr name="backgroundcolor" />
        <attr name="selectbackgroundcolor" />
    </declare-styleable>
    <declare-styleable name="TwoButtonSelectView">
        <attr name="text1" />
        <attr name="text2" />
        <attr name="textSize" />
        <attr name="backgroundcolor" />
        <attr name="selectbackgroundcolor" />
    </declare-styleable>
</resources>
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐