如何绘制平滑/圆滑的路径?

36

我正在使用path.moveTo(x, y)path.lineTo(x, y)创建路径并在每个路径中添加多条线。然后,canvas.drawPath(path, paint)将绘制所有路径。但是在某些路径中,线之间存在1-2像素的空间。如何消除这些空间?我的代码大致如下:

paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setDither(false);
paint.setStrokeWidth(3);
paint.setAntiAlias(true);

for (int i = 0; i < length; i++) {
     Path path = new Path();
     path.moveTo(a, b);
     path.lineTo(c, d);
     path.moveTo(c, d);
     path.lineTo(e, f);
     canvas.drawPath(path, paint);
}

你需要上传你的代码。 - blessanm86
你尝试过在你的画笔对象上设置抗锯齿吗? - user545680
2个回答

111
也许这会创建你想要的东西。
paint.setColor(color);                    // set the color
paint.setStrokeWidth(size);               // set the size
paint.setDither(true);                    // set the dither to true
paint.setStyle(Paint.Style.STROKE);       // set to STOKE
paint.setStrokeJoin(Paint.Join.ROUND);    // set the join to round you want
paint.setStrokeCap(Paint.Cap.ROUND);      // set the paint cap to round too
paint.setPathEffect(new CornerPathEffect(10) );   // set the path effect when they join.
paint.setAntiAlias(true);                         // set anti alias so it smooths

:)


12

您可能不想使用 lineTo(c, d) 然后立即使用 moveTo(c, d),因为那是相同的点。如果这样做,您将不会得到两条线段的漂亮拐角连接,这可能看起来像一个不好看的间隙。

试着把那个 moveTo 删除掉。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接