SkiaSharp是否具有与System.Drawing等效的Flatten方法?

时间:2017-09-27 12:52:06

标签: c# graphicspath skiasharp

我最近使用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()/

1 个答案:

答案 0 :(得分:0)

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

Oracles Technote

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;
}
...
}