Use Paint.setpathEffect (PathEffect effect) with DashPathEffect .
From the documents: the array of intervals should contain an even number of records (> = 2), with even indices defining the intervals "on" and odd indices indicating the intervals "off".
paint.setPathEffect(new DashPathEffect(new float[]{on, off}, 0));
And add this to your DrawView constructor
public DrawView(Context context) {
super(context);
paint.setColor(Color.GREEN);
paint.setPathEffect(new DashPathEffect(new float[]{on, off}, 0));
}
EDIT full code.
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.GREEN);
paint.setStyle(Style.STROKE);
}
@Override
public void onDraw(Canvas canvas) {
float width = canvas.getWidth();
float height = canvas.getHeight();
float size = height/(7*2);
paint.setStrokeWidth(WIDTH);
paint.setPathEffect(new DashPathEffect(new float[]{size, size}, 0));
for(int i = 0 ; i < 7 ; i++) {
canvas.drawLine(0, size*i,
width, size*i, paint);
canvas.drawLine(size*i, 0,
height, size*i, paint);
}
}
}
.
.
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setDither(true);
paint.setAntiAlias(true);