有没有办法像上图中那样使帽/点变圆?

java - 如何制作可绘制线条的圆形破折号或圆点-LMLPHP

<shape
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="line">

    <stroke
    android:width="2dp"
    android:color="@color/grey"
    android:dashWidth="3dp"
    android:dashGap="3dp" />

</shape>


注意

伙计们,我知道如何制作虚线,我在问如何制作“圆角”。看看这张来自Adobe XD的图片,就知道我的意思..!

java - 如何制作可绘制线条的圆形破折号或圆点-LMLPHP

最佳答案

您可以使用自定义视图并在画布上绘图来实现目标。请尝试一下,并根据您的需要调整尺寸/样式:

public class RoundedDashView extends View {

public enum Orientation {
    VERTICAL,
    HORIZONTAL
}

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Path path = new Path();
private Orientation orientation = Orientation.HORIZONTAL;

public RoundedDashView(Context context) {
    super(context);
    init();
}

public RoundedDashView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

public RoundedDashView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

public RoundedDashView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

private void init() {
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeWidth(10);
    paint.setColor(Color.GRAY);
    paint.setPathEffect(new DashPathEffect(new float[]{20, 25}, 20));
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    path.reset();
    if (orientation == Orientation.VERTICAL) {
        path.moveTo(getWidth() / 2, 0);
        path.quadTo(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight());
    } else {
        path.moveTo(0, getHeight() / 2);
        path.quadTo(getWidth() / 2, getHeight() / 2, getWidth(), getHeight() / 2);
    }
    canvas.drawPath(path, paint);
}

public void setOrientation(Orientation orientation) {
    this.orientation = orientation;
    invalidate();
}
}

08-18 12:11