android的虚线

效果图:


代码如下:

public class DashedLineViewextends View {

public DashedLineView(Context context, AttributeSet attrs) {

super(context, attrs);

    }

@Override

    protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

        Paint paint =new Paint();

        paint.setStyle(Paint.Style.STROKE);

        paint.setColor(Color.rgb(95,172,250));//颜色可以自己设置

        Path path =new Path();

        path.moveTo(0, 0);//起始坐标

        path.lineTo(0, 1200);//终点坐标

        PathEffect effects =new DashPathEffect(new float[]{8,8,8,8},1);//设置虚线的间隔和点的长度

        paint.setPathEffect(effects);

        canvas.drawPath(path, paint);

    }

}


用法:

<DashedLineView

        android:layout_marginLeft="20dp"

        android:layout_width="10dp"

        android:layout_height="match_parent"/>