先看效果
3CD2C601-38FF-458A-A257-910A3796AA19.png
创建styleable
1、在values文件夹下创建attrs.xml
2、自定义RadioButton
package com.ww7h.purchasing.main.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.view
* 创建时间: 2019/4/24 15:46
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class RemindRadioButton extends android.support.v7.widget.AppCompatRadioButton {
private float remindWidth;
private float remindHeight;
private float remindTextSize;
private int remindResourceId;
private int remindTextColor;
private int remindTextBgColor;
private int remindNumber = 0;
private float remindMarginRight;
public RemindRadioButton(Context context) {
super(context);
}
public RemindRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, com.ww7h.ww.common.R.styleable.RemindRadioButton, 0, 0);
remindMarginRight = a.getDimension(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindMarginRight, 0);
remindWidth = a.getDimension(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextWidth, 0);
remindHeight = a.getDimension(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextHeight, 0);
remindTextSize = a.getDimension(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextSize, 0);
remindResourceId = a.getResourceId(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextBackground, 0);
remindTextColor = a.getColor(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextColor, Color.WHITE);
remindTextBgColor = a.getColor(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextBgColor, Color.RED);
remindNumber = a.getInt(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindNumber, 0);
}
public RemindRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (remindNumber > 0) {
Paint paint = new Paint();
if (remindResourceId == 0) {
paint.setColor(remindTextBgColor);
}
Path path = new Path();
float minX = getWidth() - remindWidth - remindHeight / 2 - remindMarginRight;
float maxX = getWidth() - remindHeight / 2 - remindMarginRight;
// 绘制左侧圆形
path.addCircle(maxX,
remindHeight / 2, remindHeight / 2, Path.Direction.CW);
path.close();
canvas.drawPath(path, paint);
int count = 1;
// 当提醒消息数超过一位数时
if (remindNumber > 9) {
count = remindNumber > 99 ? 3 : 2;
// 绘制右侧半圆
path.addCircle(minX,
remindHeight / 2, remindHeight / 2, Path.Direction.CW);
path.close();
canvas.drawPath(path, paint);
path = new Path();
// 绘制提醒消息显示位置矩形
path.moveTo(minX, remindHeight);
path.lineTo(minX, 0);
path.lineTo(maxX, 0);
path.lineTo(maxX, remindHeight);
path.close();
canvas.drawPath(path, paint);
} else {
count = 1;
minX = getWidth() - remindHeight - remindMarginRight;
maxX = getWidth() - remindMarginRight;
path = new Path();
// 绘制消息显示矩形
path.moveTo(minX, remindHeight);
path.lineTo(minX, 0);
path.lineTo(maxX, 0);
path.lineTo(maxX, remindHeight);
path.close();
paint.setColor(Color.TRANSPARENT);
canvas.drawPath(path, paint);
}
paint.setStyle(Paint.Style.STROKE);
paint.setColor(remindTextColor);
paint.setTextSize(remindTextSize);
canvas.drawTextOnPath(String.valueOf(remindNumber), path,
remindHeight + Math.abs((remindWidth - remindTextSize / 2 * count )/2),
remindTextSize, paint);
}
}
public void setRemindNumber(int number) {
this.remindNumber = number;
invalidate();
}
public void setRemindMarginRight(float remindMarginRight) {
this.remindMarginRight = remindMarginRight;
invalidate();
}
public void setRemindWidth(float remindWidth) {
this.remindWidth = remindWidth;
invalidate();
}
public void setRemindHeight(float remindHeight) {
this.remindHeight = remindHeight;
invalidate();
}
}
3、布局中使用
4、Activity调用
package com.ww7h.purchasing.main.view;
import android.content.Intent;
import android.view.View;
import com.ww7h.common.mvvm.v.BaseVActivity;
import com.ww7h.purchasing.R;
import com.ww7h.purchasing.databinding.ActivityMainDetailBinding;
import com.ww7h.ww.common.utils.DensityUtil;
import com.ww7h.ww.common.utils.LogUtil;
import com.ww7h.ww.common.utils.ScreenUtil;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.view
* 创建时间: 2019/4/23 20:53
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class MainDetailActivity extends BaseVActivity {
@Override
protected int getContentView() {
return R.layout.activity_main_detail;
}
@Override
protected void initAction() {
vdBinding.jumpNextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, MainActivity.class);
startActivity(intent);
}
});
}
@Override
protected void initView() {
float width = ScreenUtil.Companion.getScreenWidth(activity) / 8f;
vdBinding.setNumber1(2);
vdBinding.bNav1.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav1.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav1.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 15));
vdBinding.setNumber2(23);
vdBinding.bNav2.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav2.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav2.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 20));
vdBinding.setNumber3(999);
vdBinding.bNav3.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 15));
vdBinding.bNav3.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav3.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 25));
vdBinding.setNumber4(0);
vdBinding.bNav4.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav4.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav4.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 20));
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
LogUtil.e(TAG, "-------");
}
@Override
protected void onDestroy() {
super.onDestroy();
LogUtil.e(TAG, "-------");
}
}