您可以实现View.IOnTouchListener 接口并将其作为侦听器应用到您的视图(在本例中为ImageView):
View.IOnTouchListener 实现:
注意:在此示例中使用 Java.Lang.Object 作为基类,但您可以使用任何基于Java.Lang.Object 的类(Activity 等...)
public class MyTouch : Java.Lang.Object, View.IOnTouchListener
{
TimeSpan Milli10 = TimeSpan.FromMilliseconds(10);
DateTime oldTime;
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
oldTime = DateTime.Now;
break;
case MotionEventActions.Move:
if (DateTime.Now.Subtract(oldTime) > Milli10)
{
Console.WriteLine($"Touch {e.RawX} : {e.RawY} : tD: {DateTime.Now.Subtract(oldTime)}");
oldTime = DateTime.Now;
}
break;
default:
break;
}
return true;
}
}
然后,如果需要,只需实例化侦听器,并将其应用为 OnTouchListener:
imageView = FindViewById<ImageView>(Resource.Id.background);
touch = new MyTouch();
imageView.SetOnTouchListener(touch);
更新:
需要添加 LongPress、DoubleTap 等...,子类 GestureDetector.SimpleOnGestureListener 并将其作为内部类添加到您的 View.IOnTouchListener 实现中(这只是一种方式...)
View.IOnTouchListener PLUS SimpleOnGestureListener:
public class MyTouchPlusGestures : Java.Lang.Object, View.IOnTouchListener
{
readonly MyGestures myGestures = new MyGestures();
readonly TimeSpan Milli10 = TimeSpan.FromMilliseconds(10);
readonly GestureDetector gestureDetector;
DateTime oldTime = DateTime.Now;
internal class MyGestures : GestureDetector.SimpleOnGestureListener
{
public override void OnLongPress(MotionEvent e)
{
// do something with press
base.OnLongPress(e);
}
public override bool OnDoubleTap(MotionEvent e)
{
// do something with tap
return base.OnDoubleTap(e);
}
}
public MyTouchPlusGestures(View view)
{
gestureDetector = new GestureDetector(view.Context, myGestures);
view.SetOnTouchListener(this);
}
public bool OnTouch(View v, MotionEvent e)
{
if (!gestureDetector.OnTouchEvent(e))
{
// If the event is not handled in one of your gestures,
// fall through to the MotionEventActions switch.
switch (e.Action)
{
case MotionEventActions.Down:
oldTime = DateTime.Now;
break;
case MotionEventActions.Move:
if (DateTime.Now.Subtract(oldTime) > Milli10)
{
Console.WriteLine($"Touch {e.RawX} : {e.RawY} : tD: {DateTime.Now.Subtract(oldTime)}");
oldTime = DateTime.Now;
}
break;
default:
break;
}
}
return true;
}
}
现在只需将 View 传递给您的 MyTouchPlusGestures 实例,然后为您分配手势和触摸侦听器...
imageView = FindViewById<ImageView>(Resource.Id.background);
touch = new MyTouchPlusGestures(imageView);