参考 https://blog.csdn.net/l_o_s/article/details/73550876
<com.xxx.wwww.weight.PointDividerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:PDbackgroundColor="@color/white"
app:dotColor="@color/com_red"
app:dotRadius="2dp"
app:dividerWidth="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
attrs.xml
<declare-styleable name="PointDividerView">
<!--自定义圆点的半径大小-->
<attr name="dotRadius" format="dimension" />
<!--圆点的间距-->
<attr name="dividerWidth" format="dimension" />
<!--圆点颜色-->
<attr name="dotColor" format="color" />
<!--虚线背景颜色-->
<attr name="PDbackgroundColor" format="color" />
</declare-styleable>
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import com.feishi.pettyloan.R;
/**
* https://blog.csdn.net/l_o_s/article/details/73550876
* Created by Lyf on 2017/6/5.
* 撸一串圆点做分割线 圆点分割线
*/
public class PointDividerView extends View {
private Paint mPaint; // 画笔
private float radius; // 圆的半径
private float dividerWidth; // 圆的间距
private int mColor; // 圆点的颜色
private int mBackgroundColor; // 背景颜色
private Context mContext;
public PointDividerView(Context context) {
super(context);
init(context, null);
}
public PointDividerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public PointDividerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, dip2px(10));
}
protected void init(Context context, @Nullable AttributeSet attrs) {
mContext = context;
mPaint = new Paint();
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PointDividerView);
radius = a.getDimension(R.styleable.PointDividerView_dotRadius, dip2px(1));
dividerWidth = a.getDimension(R.styleable.PointDividerView_dividerWidth, dip2px(6));
mColor = a.getColor(R.styleable.PointDividerView_dotColor, Color.parseColor("#d1d1d1"));
mBackgroundColor = a.getColor(R.styleable.PointDividerView_PDbackgroundColor, Color.TRANSPARENT);
a.recycle();
} else {
radius = dip2px(1);
dividerWidth = dip2px(6);
mColor = Color.parseColor("#d1d1d1");
mBackgroundColor = Color.TRANSPARENT;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制背景
canvas.drawColor(mBackgroundColor);
mPaint.setAntiAlias(true);
mPaint.setColor(mColor); // 设置颜色
int measuredHeight = getMeasuredHeight() / 2; // 高度居中
int measuredWidth = getMeasuredWidth();
for (float i = radius; i < measuredWidth; ) {
canvas.drawCircle(i, measuredHeight, radius, mPaint); // 小圆
i += dividerWidth;
}
}
private int dip2px(float dip) { // Metrics(满锤思)测量density(扽思提)密度
float density = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * density + 0.5f);
}
}