自定义RadioGroup 实现选择

先来张效果图



去不代码:


package com.example.demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioGroup;


/**
 * Created by Administrator on 2017/1/7 0007.
 */
public class SecletDangType extends View {
    //上下文
    private Context mContext;
//画笔
    private Paint mPaint;
//选中的位置
    private int position = 0;
//灰色开始的横坐标
    private int start = 0;
    //灰色结束的横坐标
    private int stop = 0;
    //view 宽度
    private double width;
    //文字的边界
    private Rect mBounds;

    private Paint.FontMetrics mFontMetrics;

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

    public SecletDangType(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.RED);
        mPaint.setTextSize(DensityUtil.sp2px(mContext, 16));
        mBounds = new Rect();
        mPaint.getTextBounds("手动", 0, "手动".length(), mBounds);

        mFontMetrics = mPaint.getFontMetrics();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
    }

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

        mPaint.setColor(Color.WHITE);
        mPaint.setXfermode(null);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(10);
        int end = (int) (width / 3);

        Bitmap output = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);

        Canvas bit = new Canvas(output);

        RectF rectF = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
        //画白色背景
        bit.drawRoundRect(rectF, 20, 20, mPaint);

        mPaint.setColor(Color.parseColor("#DDDDDD"));
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

        if (stop > 0) {
            end = stop;
        }
        //画选中矩形
        bit.drawRect(start, 0, end, getMeasuredHeight(), mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.parseColor("#6B6C6E"));
        //画周围圆角矩形
        bit.drawRoundRect(rectF, 20, 20, mPaint);

        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.parseColor("#DDDDDD"));
        //画分割线
        bit.drawLine((float) (width * 2 / 3), 5, (float) (width * 2 / 3), getMeasuredHeight() - 5, mPaint);
        bit.drawLine((float) (width / 3), 5, (float) (width / 3), getMeasuredHeight() - 5, mPaint);

        canvas.drawBitmap(output, 0, 0, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(2);
        //画文字
        float y = getMeasuredHeight() / 2 + (Math.abs(mFontMetrics.ascent) - mFontMetrics.descent) / 2;
        canvas.drawText("不限", (float) ((width / 3 - mBounds.width()) / 2), y, mPaint);
        canvas.drawText("手动", (float) (((width / 3 - mBounds.width()) / 2) + width / 3), y, mPaint);
        canvas.drawText("自动", (float) (((width / 3 - mBounds.width()) / 2) + width * 2 / 3), y, mPaint);
    }

    /**
     * 点击事件
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP) {
            float x = event.getX();

            if (0 < x && x < width / 3) {
                start = 0;
                stop = (int) (width / 3);
                invalidate();
                position = 0;
            } else if (width / 3 < x && x < width * 2 / 3) {
                start = (int) (width / 3);
                stop = (int) (width * 2 / 3);
                invalidate();
                position = 1;
            } else if (x > width * 2 / 3) {
                start = (int) (width * 2 / 3);
                stop = getMeasuredWidth();
                invalidate();
                position = 2;
            }
        }
        return true;
    }

    public int getPosition() {
        return position;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值