I draw a line like this:

p.setStrokeWidth(1); canvas.drawLine(crX, crY, ceX, ceY, p); 

How to give it a dotted line?

    1 answer 1

    For some effects with lines in Android there is a class PathEffect . In particular, it allows you to round the corners of the broken lines, make the line not perfectly smooth, draw a dotted line, and more.

    In order to draw a dotted line, you need the DashPathEffect class:

     class DrawView extends View { Path path; Paint p1; Paint p2; public DrawView(Context context) { super(context); path = new Path(); path.rLineTo(100, 300); p1 = new Paint(Paint.ANTI_ALIAS_FLAG); p1.setStyle(Paint.Style.STROKE); p1.setStrokeWidth(7); p2 = new Paint(p1); p2.setColor(Color.GREEN); p2.setPathEffect(new DashPathEffect(new float[] { 30, 10}, 0)); } @Override protected void onDraw(Canvas canvas) { canvas.drawARGB(80, 102, 204, 255); canvas.translate(250, 0); canvas.drawPath(path, p2); } } 

    Here, the values ​​of the array of real numbers in the argument of the DashPathEffect class are 30 dotted lines, 10 is the distance between dotted lines.
    The class works with any canvas objects that draw lines (rectangle, circle, etc.)

    Learn more about the PathEffect class.

    • Happened! Thank you - kaaa