在路径内的冲程

我想绘制一个形状,使用Path的笔画宽度为5,其中所有笔画都在Path中,而不是内部笔画的一半,外部的一半。

谢谢,

卡尔


你可以使用CornerPathEffect类来获得帮助! 以一个圆形的形状为例。

在使用canvas.drawRoundRect()方法绘制具有半径的背景颜色并使用Paint设置Style.FILL时,您可以获得圆形的矩形形状。 然后使用相同的方法用Style.STROKE和paint的设置宽度在其上绘制圆形矩形边框,即可获得边框。

代码:

mBackgroundRectF.set(0, 0, mWidth, mHeight);
canvas.drawRoundRect(mBackgroundRectF, mRadius, mRadius, mBackgroundPaint);
// edge ajustment because paint stroke style is center align
float edge = mBorderWidth / 2;
mBackgroundRectF.set(edge, edge, mWidth - edge, mHeight - edge);
canvas.drawRoundRect(mBackgroundRectF, mRadius, mRadius, mBorderPaint);

现在看起来,它不是我想要的那种,它在背景和边框之间有一些偏移:

之前

我们来试试CornerPathEffect:

mBackgroundRectF.set(0, 0, mWidth, mHeight);
canvas.drawRoundRect(mBackgroundRectF, mRadius, mRadius, mBackgroundPaint);
// edge ajustment because paint stroke style is center align
float edge = mBorderWidth / 2;
mBackgroundRectF.set(edge, edge, mWidth - edge, mHeight - edge);
// use CornerPathEffect and then use drawRect() method
mBorderPaint.setPathEffect(new CornerPathEffect(mRadius / 2));
canvas.drawRect(mBackgroundRectF, mBorderPaint);

现在看起来是正确的:


使用Canvas#clipPath(Path, Op) 。 但请注意,在Android 3.0中删除了对硬件加速画布中路径的剪切支持,并在4.3中重新引入。 显然是3.0-4.2的解决方法,但我没有办法测试它。


看起来它无法控制中风的位置(即内侧,中间或外侧)。 有关更多信息,请参阅:Android画笔描边宽度定位

我的解决方案是在绘制例如,

final RectF rectF = new RectF(halfStrokeWidth, halfStrokeWidth, width - halfStrokeWidth, height - halfStrokeWidth);
canvas.drawRoundRect(rectF, roundX, roundY, paint);
链接地址: http://www.djcxy.com/p/13861.html

上一篇: stroke inside path

下一篇: data size or something else?