如何使用SkiaSharp在Android中添加OnTouch事件 [英] How to add OnTouch Event in android with SkiaSharp
问题描述
我有问题.我正在使用SkiaSharp制作TriangleGrid.现在,我正在绘制网格,但是现在我想触摸网格中的三角形以为其着色.为此,我需要在SKCanvasView中添加一个TouchEvent,但我不知道该怎么做.
在互联网上,我可以找到下一个示例:
I have a problem. I am using SkiaSharp to make a TriangleGrid. Now I am doing drawing the Grid, but now I want to touch a triangle in the grid to color it. To do that I need to add a TouchEvent to the SKCanvasView, but I dont know how to do that.
On the internet I can find the next example:
- https ://docs.microsoft.com/zh-CN/xamarin/xamarin-forms/user-interface/graphics/skiasharp/paths/finger-paint
- https://github.com/mattleibow/SkiaSharpDemo/blob/master/SkiaSharpDemo/SkiaSharpDemo/MainPage.xaml
- https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/paths/finger-paint
- https://github.com/mattleibow/SkiaSharpDemo/blob/master/SkiaSharpDemo/SkiaSharpDemo/MainPage.xaml
但是我正在使用Xamarin Android,而这些示例在我的代码中不起作用.我也尝试使用:
But I am using Xamarin Android and those example doesn't work in my code. I also tried to use:
skiaView = FindViewById<SkiaSharp.Views.Android.SKCanvasView>(Resource.Id.skiaView);
skiaView.SetOnTouchListener += OnTouch;
但这给了我一个错误:由于它是一个方法组",因此无法分配给'SetOnTouchListener'"
But that gives me the error: "Cannot assign to 'SetOnTouchListener' because it's a 'method group'"
有人可以帮我在SkiaSharp画布上获得TouchListener吗??
Can anyone help me to get a TouchListener on my SkiaSharp canvas!?
推荐答案
您使用错误的方法添加了事件,可以这样设置OnTouch
事件:
You are using the wrong method to add event ,you could set OnTouch
event like this:
skiaView= FindViewById<SKCanvasView>(Resource.Id.skiaView);
skiaView.Touch += onTouch;
private void onTouch(object sender, View.TouchEventArgs e)
{
Toast.MakeText(this, "touch", ToastLength.Short).Show();
}
或:
public class MainActivity : AppCompatActivity,View.IOnTouchListener
{
private SKCanvasView skiaView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
skiaView= FindViewById<SKCanvasView>(Resource.Id.skiaView);
skiaView.SetOnTouchListener(this);
...
}
public bool OnTouch(View v, MotionEvent e)
{
Toast.MakeText(this,"touch",ToastLength.Short).Show();
return true;
}
}
这篇关于如何使用SkiaSharp在Android中添加OnTouch事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!