如何使用 SkiaSharp 在 android 中添加 OnTouch 事件
How to add OnTouch Event in android with SkiaSharp
我有问题。我正在使用 SkiaSharp 制作 TriangleGrid。现在我正在绘制网格,但现在我想触摸网格中的一个三角形来为其着色。为此,我需要向 SKCanvasView 添加 TouchEvent,但我不知道该怎么做。
在互联网上我可以找到下一个示例:
- 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,这些示例在我的代码中不起作用。我也尝试使用:
skiaView = FindViewById<SkiaSharp.Views.Android.SKCanvasView>(Resource.Id.skiaView);
skiaView.SetOnTouchListener += OnTouch;
但这给了我错误:"Cannot assign to 'SetOnTouchListener' because it's a 'method group'"
谁能帮我在我的 SkiaSharp 上安装 TouchListener canvas!?
您使用了错误的方法来添加事件,您可以这样设置 OnTouch
事件:
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 制作 TriangleGrid。现在我正在绘制网格,但现在我想触摸网格中的一个三角形来为其着色。为此,我需要向 SKCanvasView 添加 TouchEvent,但我不知道该怎么做。
在互联网上我可以找到下一个示例:
- 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,这些示例在我的代码中不起作用。我也尝试使用:
skiaView = FindViewById<SkiaSharp.Views.Android.SKCanvasView>(Resource.Id.skiaView);
skiaView.SetOnTouchListener += OnTouch;
但这给了我错误:"Cannot assign to 'SetOnTouchListener' because it's a 'method group'"
谁能帮我在我的 SkiaSharp 上安装 TouchListener canvas!?
您使用了错误的方法来添加事件,您可以这样设置 OnTouch
事件:
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;
}
}