Android PathDashPathEffect: path shape does not work for straight line
I'm trying to create a custom view that draws a line with diagonal stripes pattern. It should look something like this:
I think using PathDashPathEffect should do, but when I set the shape of the path to be a direct path, the view draws nothing. If I add some segments to the path to close it, for example make it a triangle, then a view appears.
Here's my path form:
Path pathShape = new Path();
pathShape.moveTo(0, 10);
pathShape.lineTo(10, 0);
The above doesn't work, the view doesn't show anything. The following works and shows triangles as a pattern on a line:
Path pathShape = new Path();
pathShape.moveTo(0, 0);
pathShape.lineTo(10, 10);
pathShape.lineTo(10, 0);
My whole custom view:
private final Paint paint;
private final Path path;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(20);
paint.setColor(Color.RED);
Path pathShape = new Path();
pathShape.moveTo(0, 0);
pathShape.lineTo(10, 10);
pathShape.lineTo(10, 0);
PathDashPathEffect pathEffect = new PathDashPathEffect(pathShape, 30, 30, PathDashPathEffect.Style.ROTATE);
paint.setPathEffect(pathEffect);
path = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
path.reset();
path.moveTo(0, getHeight() / 2);
path.lineTo(getWidth(), getHeight() / 2);
canvas.drawPath(path, paint);
}
Does anyone know how to achieve this?
Many thanks!
+3
source to share
1 answer