简体   繁体   English

以Xamarin形式在SkiaSharp画布上居中对齐圆

[英]Center align circle on SkiaSharp Canvas in Xamarin Forms

I have following code which paints a circle on a canvas, in Xamarin Forms. 我有以下代码,以Xamarin Forms在画布上绘制一个圆圈。 I am specifying the position of the center of the circle as shown below. 我正在指定圆心的位置,如下所示。

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

What is the correct way to align the circle on the center of the canvas without specifying center position? 在不指定中心位置的情况下将圆对齐到画布中心的正确方法是什么?

Note: I tried SKTextAlign.Center property of SKPaint inside circleFill and circleBorder. 注:我试过SKTextAlign.Center财产SKPaint内circleFill和circleBorder。 But that didn't help 但这没有帮助

    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 XAML

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

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

var x = e.Info.Width / 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM