圆点虚线 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);
    }
}
相关推荐
API_technology12 分钟前
电商API安全防护:JWT令牌与XSS防御实战
前端·安全·xss
yqcoder17 分钟前
Express + MongoDB 实现在筛选时间段中用户名的模糊查询
java·前端·javascript
十八朵郁金香39 分钟前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
m0_528723812 小时前
HTML中,title和h1标签的区别是什么?
前端·html
Dark_programmer2 小时前
html - - - - - modal弹窗出现时,页面怎么能限制滚动
前端·html
CYRUS_STUDIO2 小时前
使用 AndroidNativeEmu 调用 JNI 函数
android·逆向·汇编语言
GDAL2 小时前
HTML Canvas clip 深入全面讲解
前端·javascript·canvas
禾苗种树2 小时前
在 Vue 3 中使用 ECharts 制作多 Y 轴折线图时,若希望 **Y 轴颜色自动匹配折线颜色**且无需手动干预,可以通过以下步骤实现:
前端·vue.js·echarts
梦否2 小时前
【Android】类加载器&热修复-随记
android
贵州数擎科技有限公司2 小时前
使用 Three.js 实现流光特效
前端·webgl