使用Xamarin.Forms中的SkiaSharp将点击事件添加到SVG中的元素

时间:2019-07-01 12:52:46

标签: xamarin xamarin.forms xamarin.android

我想获取xamarin.forms的svg中的特定点 有什么方法可以实现?

谢谢。

1 个答案:

答案 0 :(得分:0)

是的,您可以使用Skiasharp将触摸事件添加到特定点。您只需要按位置过滤操作即可。假设您正在使用Xaml和C#,那么您的Xaml代码将如下所示:

<Grid BackgroundColor="White"
        Grid.Row="1">

    <skia:SKCanvasView x:Name="canvasView"
                        PaintSurface="OnCanvasViewPaintSurface" />
    <Grid.Effects>
        <tt:TouchEffect Capture="True"
                        TouchAction="OnTouchEffectAction" />
    </Grid.Effects>
</Grid>

然后您的C#就是这样

void OnTouchEffectAction(object sender, TouchActionEventArgs args)
{
    // Convert Xamarin.Forms point to pixels
    Point pt = args.Location;
    SKPoint point = 
        new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width),
                    (float)(canvasView.CanvasSize.Height * pt.Y / canvasView.Height));

    // Uncomment the code below based on what you specifically need
    // if (point != The Point you care about) or (pt != The Location you care about
        // return;
    switch (args.Type)
    {
        case TouchActionType.Pressed:
            if (bitmap.HitTest(point))
            {
                touchIds.Add(args.Id);
                bitmap.ProcessTouchEvent(args.Id, args.Type, point);
                break;
            }
            break;

        case TouchActionType.Moved:
            if (touchIds.Contains(args.Id))
            {
                bitmap.ProcessTouchEvent(args.Id, args.Type, point);
                canvasView.InvalidateSurface();
            }
            break;

        case TouchActionType.Released:
        case TouchActionType.Cancelled:
            if (touchIds.Contains(args.Id))
            {
                bitmap.ProcessTouchEvent(args.Id, args.Type, point);
                touchIds.Remove(args.Id);
                canvasView.InvalidateSurface();
            }
            break;
    }
}

Charles Petzold与Skiasharp一起撰写了these Github examples,请检查一下以获得更好的主意