Most of the time, the dotted line is painted using a chopped graph (a real point, a virtual point), and then using the repeat attribute of Bitmap
Next we will draw a dotted line, first define a dashedline class, inherit from the View, override the Ondraw () method
Public class DashedLine extends View {private final String namespace = "http://www.android-study.com/"; private float startX; private float startY; private float endX; private float endY; private Rect mRect; public DashedLine (Context context, AttributeSet attrs) {super (context, attrs) ;}@ Overrideprotected void onDraw (Canvas canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); Paint paint = new Paint (); paint. setStyle (Paint. style. STROKE); // hollow paint. setColor (Color. DKGRAY); Path path = new Path (); // use the x and y coordinates of moveto and lineto to determine whether the dotted line is horizontal, vertical, or skewed path. moveTo (0, 10); // Set the beginning of the next contour to the point (x, y) path. lineTo (480, 10); // Add a line from the last point to the specified point (x, y ). // DashPathEffect can be used to create a dotted line outline (short horizontal line/small dot), instead of using solid line // float [] {5, 5, 5, 5} value control dotted line spacing, density PathEffect effects = new DashPathEffect (new float [] {5, 5, 5}, 1); paint. setPathEffect (effects); canvas. drawPath (path, paint );}}
After defining the View, you can use it in XML.