0

我最近使用 SkiaSharp 遇到了障碍。我一直在将旧代码从 System.Drawing 移植到 SkiaSharp。GraphicsPath 类有一个 flatten 方法,可以将曲线转换为一系列连接的线段。

https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath.flatten(v=vs.110).aspx

我似乎找不到 SkiaSharp 的等效方法。SKPath Simplify 方法看起来有点模糊它的实际作用。在我的测试中,它似乎没有产生平坦的路径。

https://developer.xamarin.com/api/member/SkiaSharp.SKPath.Simplify()/

4

1 回答 1

1

在我提出这个问题后的某个时候,Xamarin 添加了文档来执行此操作。它位于这里

https://developer.xamarin.com/guides/xamarin-forms/advanced/skiasharp/curves/information/

static class PathExtensions
{
public static SKPath CloneWithTransform(this SKPath pathIn, Func<SKPoint, SKPoint> transform)
{
    SKPath pathOut = new SKPath();

    using (SKPath.RawIterator iterator = pathIn.CreateRawIterator())
    {
        SKPoint[] points = new SKPoint[4];
        SKPathVerb pathVerb = SKPathVerb.Move;
        SKPoint firstPoint = new SKPoint();
        SKPoint lastPoint = new SKPoint();

        while ((pathVerb = iterator.Next(points)) != SKPathVerb.Done)
        {
            switch (pathVerb)
            {
                case SKPathVerb.Move:
                    pathOut.MoveTo(transform(points[0]));
                    firstPoint = lastPoint = points[0];
                    break;

                case SKPathVerb.Line:
                    SKPoint[] linePoints = Interpolate(points[0], points[1]);

                    foreach (SKPoint pt in linePoints)
                    {
                        pathOut.LineTo(transform(pt));
                    }

                    lastPoint = points[1];
                    break;

                case SKPathVerb.Cubic:
                    SKPoint[] cubicPoints = FlattenCubic(points[0], points[1], points[2], points[3]);

                    foreach (SKPoint pt in cubicPoints)
                    {
                        pathOut.LineTo(transform(pt));
                    }

                    lastPoint = points[3];
                    break;

                case SKPathVerb.Quad:
                    SKPoint[] quadPoints = FlattenQuadratic(points[0], points[1], points[2]);

                    foreach (SKPoint pt in quadPoints)
                    {
                        pathOut.LineTo(transform(pt));
                    }

                    lastPoint = points[2];
                    break;

                case SKPathVerb.Conic:
                    SKPoint[] conicPoints = FlattenConic(points[0], points[1], points[2], iterator.ConicWeight());

                    foreach (SKPoint pt in conicPoints)
                    {
                        pathOut.LineTo(transform(pt));
                    }

                    lastPoint = points[2];
                    break;

                case SKPathVerb.Close:
                    SKPoint[] closePoints = Interpolate(lastPoint, firstPoint);

                    foreach (SKPoint pt in closePoints)
                    {
                        pathOut.LineTo(transform(pt));
                    }

                    firstPoint = lastPoint = new SKPoint(0, 0);
                    pathOut.Close();
                    break;
            }
        }
    }
    return pathOut;
}
...
}
于 2018-01-25T14:59:28.607 回答