圆点虚线 Android

参考 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);
    }
}
相关推荐
Mike_jia7 分钟前
SSM平台:Ansible与Docker融合的运维革命——轻量级服务器智能管理指南
前端
yinuo8 分钟前
Uni-App跨端开发实战:编译微信小程序跳转全平台终极指南(01)
前端
小流苏生11 分钟前
或许,找对象真的太难了……
前端·后端·程序员
前端小巷子20 分钟前
Vue 3 模板编译器
前端·vue.js·面试
江城开朗的豌豆22 分钟前
为什么在render里调setState,代码会和你“翻脸”?
前端·javascript·react.js
江城开朗的豌豆24 分钟前
子组件改状态,父组件会“炸毛”吗?
前端·javascript·react.js
晓得迷路了24 分钟前
栗子前端技术周刊第 96 期 - Rspack v1.5、ESLint v9.34.0、Bun v1.2.1...
前端·javascript·bun
Sapphire~25 分钟前
重学JS-004 --- JavaScript算法与数据结构(四)JavaScript 表单验证
前端·javascript·数据结构·算法
程序视点32 分钟前
局域网文件传输神器LocalSend:比微信QQ更快更安全的跨平台传输方案
前端·后端
上等猿33 分钟前
JUC多线程个人笔记
android·java·笔记