SkiaSharp - 如何在曲线之间的填充 space 上创建影线或虚线图案

SkiaSharp - how to create a hatch or dotted pattern on the filled space between curves

在 SkiaSharp 中,我可以使用 SKPathFillType.EvenOdd 很好地填充两条曲线之间的 space。下面我展示了代码的简化摘录。 我的问题是如何为曲线之间的填充区域赋予特定图案?这里我只能给它填充一种颜色,并给它一个透明度。我对应用图案感兴趣,例如影线或圆点。

感谢您的支持。

你好,

索林

SKPath path = new SKPath();
path.FillType = SKPathFillType.EvenOdd;

// start the first curve
path.MoveTo(....);
path.LineTo(....); // draw the curve and close it
....

path.AddCircle(....); // add a second curve as a circle

SKPaint paint = new SKPaint(new SKFont(SKTypeface.Default)) {
                IsAntialias = true,
                Style = SKPaintStyle.Fill,
                Color = SKColors.Blue.WithAlpha((byte)(0xFF * (1 - 0.5))),
                StrokeWidth = 1
            };

canvas.DrawPath(path, paint);

我设法用一个技巧解决了这个问题。 首先,我做了我上面写的所有内容,即

canvas.DrawPath(path, paint)

.....会在两条曲线之间绘制一个填充区域,具有一定的透明度。 最重要的是(字面上),我画了另一个图案:

var hatch = new SKPath();
hatch.AddCircle(0, 0, 1);
var hatchPaint = new SKPaint {
   PathEffect = SKPathEffect.Create2DPath(SKMatrix.MakeScale(7, 7), hatch),
   Color = SKColors.RosyBrown,
   Style = SKPaintStyle.Stroke,
   StrokeWidth = 3
};

再一次:

 canvas.DrawPath(path, hatchPaint);

这会在曲线之间的填充区域顶部绘制一个漂亮的填充图案。 注意:图案的大小是必不可少的 - 这里是 AddCircle(0, 0, 1),其中圆形光线为 1 个像素。如果你有一个更大的,填充图案会溢出填充区域,这不是你想要的。对我来说,这看起来像是 SKIA 中的一个错误。