Tool classes used:
Dashpatheffect
The description of the class in Android is as follows:
The translation is:
The first parameter intervals array must be an even number of elements (number >=2), even subscript represents a solid line, and an odd subscript represents a blank length
The second parameter phase: Left offset (offset =phase mod (sum of intervals array items), i.e. take-away operation)
Note: The dashed effect can only be drawn when the style of paint is set to stroke or fill_and_stroke, and will not work if the paint style is set to fill.
/*** The intervals array must contain an even number of entries (>=2), with * The even indices specifying the "On" intervals, and the odd indices * specifying the "off" intervals. Phase is a offset into the intervals * Array (mod the sum of all of the intervals). The intervals array * Controls the length of the dashes. The paint ' s strokewidth controls the * thickness of the dashes. * Note:this Patheffect only affects drawing with the paint ' s style are set * to STROKE or fill_and_stroke. It is ignored if the drawing is do with * style = = FILL. * @paramintervals array of on and OFF distances *@paramphase offset into the intervals array*/ PublicDashpatheffect (floatIntervals[],floatphase) { if(Intervals.length < 2) { Throw Newarrayindexoutofboundsexception (); } native_instance=nativecreate (intervals, phase); }
Initialize Paint, Path
Private Paint Mdottedlinepaint; Private Path Mdottedpath; // Dashed Paint New Paint (); Mdottedlinepaint.setcolor (Getresources (). GetColor (R.color.horizontal_bar_chart_dot_line_color)); Mdottedlinepaint.setstyle (Style.stroke); Mdottedlinepaint.setstrokewidth (2new Dashpatheffect (newfloat[]{, 5}, 1new Path ();
Draw a dashed line
Canvas.drawpath (Mdottedpath, mdottedlinepaint);
Here need to pay attention to, must use the DrawPath method of canvas , if use drawline words is to draw the effect of, originally in the Internet to find the code is used DrawLine, the results of half a day not to effect
Android Draw dashed Line