在Xamarin Forms中的SkiaSharp Canvas上居中对齐圆圈

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

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

如何在不指定中心位置的情况下将圆圈对齐画布中心的正确方法是什么?

注意:我在circleFill和circleBorder中尝试了 SKTextAlign.Center 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>

回答(1)

2 years ago

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

var x = e.Info.Width / 2;