自定义带红点的radioButton

 <com.ycx.driver.utils.widget.view.CustomRadioButton
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="#00ffff"
            android:button="@null"
            android:padding="50dp"
            android:drawablePadding="10dp"
            android:text="个人"
            customRadioButton:drawableTop="@drawable/tab_profile_click" />
 <declare-styleable name="CustomRadioButton">
        <attr name="drawableTop" format="reference|color" />
    </declare-styleable>
public class CustomRadioButton extends RadioButton {

    Paint paint = new Paint();

    private int redius = 10;

    private Bitmap bitmap;
    private int bitmapWidth;
    private int bitmapHeight;

    public CustomRadioButton(Context context) {
        super(context);
        init(context, null);
    }

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

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

    private void init(Context context, AttributeSet attrs) {
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(getTextSize());

        setGravity(Gravity.CENTER_HORIZONTAL);
        try {
            // Popup信息
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.header_man);
            if (bitmap != null) {
                bitmapWidth = bitmap.getWidth();
                bitmapHeight = bitmap.getHeight();
            }
        } catch (Throwable e) {
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize + redius;
        } else {
            width = 200 + redius;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize + redius;
        } else {
            height = 200 + redius;
        }
        setMeasuredDimension(width, height);
    }

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

        int left = (getWidth() - bitmapWidth) / 2;
        int top = (getHeight() - bitmapHeight) / 2;
        int right = (getWidth() + bitmapWidth) / 2;
        int bottom = (getHeight() + bitmapHeight) / 2;
        if (left > 0 && top > 0 && bitmap != null) {
            canvas.drawBitmap(bitmap, left, top, null);
        }

        //绘制文字
        float textWidth = paint.measureText(getText().toString());
        canvas.drawText(getText().toString(), getWidth() / 2 - textWidth / 2, bottom +40, paint);

        paint.setColor(Color.RED);
        canvas.drawCircle(right, top, redius, paint);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值