我正在尝试构建一个自定义视图,用斜条纹图案绘制一条线。它应该类似于:
我认为使用 PathDashPathEffect 应该可以,但是当我将路径形状设置为直线路径时,视图不会绘制任何内容。如果我向路径添加更多段以便它可以关闭,例如将其设为三角形,则视图会显示。
这是我的路径形状:
Path pathShape = new Path();
pathShape.moveTo(0, 10);
pathShape.lineTo(10, 0);
以上不起作用,视图没有显示任何内容。下面的作品并将三角形显示为线上的图案:
Path pathShape = new Path();
pathShape.moveTo(0, 0);
pathShape.lineTo(10, 10);
pathShape.lineTo(10, 0);
我的整个自定义视图:
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);
}
有谁知道如何实现这一目标?
非常感谢!