Draw dashed lines on the abdroid screen, most commonly the custom control Dashedline, and then put the custom control into the XML layout
Run:
Program Structure
Packagecom.example.asus.gary_042;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.DashPathEffect;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Path;ImportAndroid.graphics.PathEffect;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;/*** Created by ASUS on 2018/5/26.*/ Public classDashedlineextendsview{ Publicdashedline (Context context,attributeset attrs) {Super(Context,attrs); } protected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Paint Paint=NewPaint (); Paint.setstyle (Paint.Style.STROKE); Paint.setcolor (Color.Black); Path Path=NewPath (); Path.moveto (0,200); Path.lineto (1280,200); Patheffect Effects=NewDashpatheffect (New float[]{5,5,5,5},1); Paint.setpatheffect (effects); Canvas.drawpath (Path,paint); }}
Dashedline
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:app=" Http://schemas.android.com/apk/res-auto " xmlns:tools=" http/ Schemas.android.com/tools " android:layout_width=" Match_parent " android:layout_height = "Match_parent" tools:context= "Com.example.asus.gary_042.MainActivity" > < Com.example.asus.gary_042.DashedLine Android:id= "@+id/dashedline" android:layout_width = "Match_parent" android:layout_height= "Match_parent"/></linearlayout>
Activity_main.xml
One, custom control dashedline, use this control to draw a dashed line in the screen
protected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Paint Paint=NewPaint (); //set the style (effect) for path, Storke set the dashed linePaint.setstyle (Paint.Style.STROKE); //Set Dash colorPaint.setcolor (Color.Black); Path Path=NewPath (); //starting pointPath.moveto (0,200); //EndPath.lineto (1280,200); //then one unit of the dashed line is a virtual segment composed of 5 pixels solid, 5 pixels blank, 5 pixels solid, 5 pixels blank. Patheffect effects =NewDashpatheffect (New float[]{5,5,5,5},1); //put a style in a linepaint.setpatheffect (effects); Canvas.drawpath (Path,paint); }
Canvas.drawpath method
The path class consists of a line, two curves, and three curves that make up a variety of conforming set path graphs, which can be drawn with Canvas.drawpath () and can be filled or stroked (based on the paint style). And it can be used to crop or draw text on a path.
Canvas drawLine
has only methods and no drawDashLine
methods. But you know, the painting is decided by the canvas, but the painting is determined by the brush paint.
Paint has
setPathEffect(PathEffect effect)
Such a method, Patheffect altogether has six sub-classes: Composepatheffect, Cornerpatheffect, Dashpatheffect, Discretepatheffect, Pathdashpatheffect, Sumpatheffect, of which theDashpatheffectIs the dash effect we need
Second, introduce the custom control in the Activity_main file dashedline add an absolute path that references the location of the custom space <com.example.asus.gary_042.dashedline
>
<com.example.asus.gary_042.DashedLine android:id= "@+id/dashedline" android:layout_ Width= "Match_parent" android:layout_height= "Match_parent" />
Android_ (Control) use a custom control to draw a dashed line in the screen