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

时间:2017-07-12 22:11:54

标签: xamarin xamarin.forms skiasharp

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

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

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

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

    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 个答案:

答案 0 :(得分:2)

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

var x = e.Info.Width / 2;