javaandroidandroid-layoutandroid-drawablexml-drawable

How to make a rounded dashes or dots for line drawable shape


Is there a way that can I make the cap/dot rounded, like in the attached picture?

enter image description here

<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>

NOTE

Guys, I know how to make a dotted line, I'm asking how to make a "rounded" dashes.!! look at this picture from Adobe XD to know what I mean..!

enter image description here


Solution

  • You can achieve a goal using a custom view and drawing on canvas. Please, try this and adjust sizes/styling for your needs:

    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();
    }
    }