圆点虚线 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);
    }
}
相关推荐
NightCyberpunk34 分钟前
HTML、CSS
前端·css·html
xcLeigh44 分钟前
HTML5超酷响应式视频背景动画特效(六种风格,附源码)
前端·音视频·html5
1024小神1 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
zhenryx1 小时前
前端-react(class组件和Hooks)
前端·react.js·前端框架
ZwaterZ1 小时前
el-table-column自动生成序号&&在序号前插入图标
前端·javascript·c#·vue
兰琛1 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法2 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
zhangjr05753 小时前
【HarmonyOS Next】鸿蒙实用装饰器一览(一)
前端·harmonyos·arkts
NotesChapter3 小时前
Android吸顶效果,并有着ViewPager左右切换
android
不爱学习的YY酱3 小时前
【操作系统不挂科】<CPU调度(13)>选择题(带答案与解析)
java·linux·前端·算法·操作系统