Xamarin Forms 中 SkiaSharp Canvas 的中心对齐圆

Center align circle on SkiaSharp Canvas in Xamarin Forms

我有以下代码,在 Xamarin Forms 中在 canvas 上绘制一个圆圈。我正在指定圆心的位置,如下所示。

  canvas.DrawCircle(100, 100, 150, circleFill);

在不指定中心位置的情况下,将圆对准 canvas 中心的正确方法是什么?

注意:我在 circleFill 和 circleBorder 中尝试了 SKTextAlign.Center 属性 of SKPaint。但这并没有帮助

    private void OnPainting(object sender, SKPaintSurfaceEventArgs e)
    {
        var surface = e.Surface;
        var canvas = surface.Canvas;
        canvas.Clear(SKColors.White);

        ///GRADIENT--------------------------------------------------------------------
        var colors = new SKColor[] { new SKColor(6, 107, 249), new SKColor(27, 162, 210) , new SKColor(36, 182, 197) };
        var shader = SKShader.CreateLinearGradient(new SKPoint(300, 0), new SKPoint(300, 600), colors, null, SKShaderTileMode.Clamp);
        var paint = new SKPaint() { Shader = shader };
        canvas.DrawPaint(paint);

        // DRAWING SHAPES
        var circleFill = new SKPaint
        {
            IsAntialias = true,
            Style = SKPaintStyle.Fill, //FILL
            Color = SKColors.Blue
        };
        canvas.DrawCircle(100, 100, 150, circleFill);

        var circleBorder = new SKPaint
        {
            IsAntialias = true,
            Style = SKPaintStyle.Stroke, //STROKE
            Color = SKColors.Teal,
            StrokeWidth = 15
        };
        canvas.DrawCircle(100, 100, 150, circleBorder);
    }

XAML

<Grid>
    <views:SKCanvasView PaintSurface="OnPainting" EnableTouchEvents="True" Touch="OnTouch">
    </views:SKCanvasView>
</Grid>

您可以根据canvas大小计算位置,例如

var x = e.Info.Width / 2;