自定义带红点的radioButton

xiaoxiao2021-02-27  223

<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); }
转载请注明原文地址: https://www.6miu.com/read-9190.html

最新回复(0)