Draw spline(smooth path) with SkiaSharp lib on Xamarin.Forms?

你说的曾经没有我的故事 提交于 2020-06-26 06:24:39

问题


I'm making app with using Xamarin forms.

I want to draw a smooth path (spline) with SKPath, but I am unable to find a simple way to achieve this.

Skiasharp does not support it currently unless I make path smoothly myself.

Thanks.


回答1:


SkiaSharp supports cubic Bezier curves. So, what you can do, is divide the spline into segments (points determine segment endpoints), and draw each segment using a cubic Bezier curve. To do this, you need to introduce two control points between each point in your spline curve. These points need to be selected so that the curves in neighboring segments connect smoothly.

I just did an implementation of this. The public static SKPath CreateSpline(params SKPoint[] Points) method generates a path that you can use to draw a spline between points. You can view the implementation here:

https://github.com/PeterWaher/IoTGateway/blob/master/Script/Waher.Script.Graphs/Functions/Plots/Plot2DCurve.cs

Example:




回答2:


I am not sure exactly what you are asking, but there are two areas for smoothing:

Anti-aliasing using SKPaint.IsAntialias:

var paint = new SKPaint {
    IsAntialias = true
};
canvas.DrawPath (path, paint);

Path Curves:

If you need the curves in the path, such as round corners, you will need to use the various curve methods. These include, but are not limited to, SKPath.ArcTo() and 'SKPath.CubicTo():

var path = new SKPath();
path.MoveTo(10, 10);
path.ArcTo(SKRect.Create(10, 10, 30, 30), 0, 45);

Path docs: https://developer.xamarin.com/api/type/SkiaSharp.SKPath/



来源:https://stackoverflow.com/questions/41802311/draw-splinesmooth-path-with-skiasharp-lib-on-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!