圆点虚线 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);
    }
}
相关推荐
转转技术团队21 小时前
转转商品中心微前端升级之路
前端
love530love21 小时前
【笔记】解决 ComfyUI 安装节点 ComfyUI-Addoor (葵花宝典)后启动报错:No module named ‘ComfyUI-Addoor’
linux·运维·前端·人工智能·windows·笔记·python
zzywxc78721 小时前
解锁 Rust 开发新可能:从系统内核到 Web 前端的全栈革命
开发语言·前端·python·单片机·嵌入式硬件·rust·scikit-learn
知新坊21 小时前
RustDesk 完整部署教程:支持 Web 管理后台和网页客户端远程,保姆级教学来了!
前端
敲敲了个代码1 天前
UniApp 多页面编译优化:编译时间从10分钟到1分钟
开发语言·前端·javascript·学习·uni-app
czhc11400756631 天前
Linux1023 mysql 修改密码等
android·mysql·adb
景彬1 天前
小红书小组件开发 最早踩坑版
前端·微信小程序
mapbar_front1 天前
今天聊聊面试
前端·面试
华仔啊1 天前
Vue3+CSS实现一个非常丝滑的 input 标签上浮动画,设计师看了都点赞
前端·css·vue.js
GOATLong1 天前
MySQL内置函数
android·数据库·c++·vscode·mysql