PackageCom.yesway.ycarplus.view;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.DashPathEffect;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Paint.Style;ImportAndroid.graphics.Path;ImportAndroid.graphics.PathEffect;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;/*** Draw a view with a dashed line * *@authorLiulm **/ Public classDottedlineextendsView {Private intcolor =Color.White; PublicDottedline (Context context, AttributeSet attrs) {Super(context, attrs); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {setmeasureddimension (Widthmeasurespec, Heightmeasurespec); Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } @SuppressLint ("Drawallocation") @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Paint P=NewPaint (); P.setantialias (true); P.setstyle (Style.stroke); P.setcolor (color); P.setstrokewidth (10); Patheffect Effects=NewDashpatheffect (New float[] {0, 10, 35, 2}, 1); Path Path=NewPath (); Path.moveto (0, 0); Path.lineto (0, Getmeasuredheight ()); Path.lineto (Getmeasuredwidth (), Getmeasuredheight ()); P.setpatheffect (effects); Canvas.drawpath (path, p); } /*** Set Color * *@paramColor*/ Public voidSetColor (intcolor) { This. color =color; Invalidate (); } /*** Set Color value * *@paramColor*/ Public voidsetcolor (String color) { This. color =Color.parsecolor (Color); Invalidate (); }}
Android Draw dashed Custom view