圆点虚线 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);
    }
}
相关推荐
gx23486 小时前
MySQL-5-触发器和储存过程
android·mysql·adb
IT_陈寒6 小时前
Vue 3响应式原理深度拆解:5个90%开发者不知道的Ref与Reactive底层实现差异
前端·人工智能·后端
睡前要喝豆奶粉7 小时前
在.NET Core Web Api中使用JWT并配置UserContext获取用户信息
前端·.netcore
前端加油站7 小时前
一份实用的Vue3技术栈代码评审指南
前端·vue.js
Jonathan Star13 小时前
沉浸式雨天海岸:用A-Frame打造WebXR互动场景
前端·javascript
工业甲酰苯胺14 小时前
实现 json path 来评估函数式解析器的损耗
java·前端·json
老前端的功夫14 小时前
Web应用的永生之术:PWA落地与实践深度指南
java·开发语言·前端·javascript·css·node.js
LilySesy14 小时前
ABAP+WHERE字段长度不一致报错解决
java·前端·javascript·bug·sap·abap·alv
六件套是我14 小时前
redission实现延时队列
android·java·servlet
Wang's Blog15 小时前
前端FAQ: Vue 3 与 Vue 2 相⽐有哪些重要的改进?
前端·javascript·vue.js