[英]How to track a finger's location during movement?
Suppose there is an ImageView (iv) and I set iv.SetOnTouchListener(this)
while creating a new GestureDetector (gs) gs = new GestureDetector(this.Context, listener)
, how could I tack the finger's location (x,y) in each 0.01 seconds (as an example)? 假设有一个ImageView(iv),并且在创建新的GestureDetector(gs
iv.SetOnTouchListener(this)
时设置了iv.SetOnTouchListener(this)
gs = new GestureDetector(this.Context, listener)
,我该如何在以下位置添加手指的位置(x,y)每0.01秒(例如)?
Which function should I use? 我应该使用哪个功能?
OnFling
? OnFling
? OnLongPress
? OnLongPress
?
I'm not talking only about the code, but I'd also like to get an idea of how to implement this desire of getting the finger's position in each 0.01 seconds. 我不仅在讨论代码,而且还想了解如何实现这种希望每0.01秒获得一次手指位置的愿望。 Any ideas?
有任何想法吗?
You can implement the View.IOnTouchListener
interface and apply it as a listener to your View ( ImageView
in this case): 您可以实现
View.IOnTouchListener
接口,并将其作为侦听器应用于您的View(在本例中为ImageView
):
Note: Using Java.Lang.Object as the base class in this example, but you can use any Java.Lang.Object
-based class (Activity, etc...) 注意:在此示例中,将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;
}
}
Then just instance the listener, if needed, and apply it as a the OnTouchListener
: 然后,如果需要,只需实例化侦听器,然后将其作为
OnTouchListener
:
imageView = FindViewById<ImageView>(Resource.Id.background);
touch = new MyTouch();
imageView.SetOnTouchListener(touch);
Need to add LongPress, DoubleTap, etc..., subclass GestureDetector.SimpleOnGestureListener
and add it as an inner class to your View.IOnTouchListener implementation (this is just one way...) 需要添加LongPress,DoubleTap等...,子类
GestureDetector.SimpleOnGestureListener
并将其作为内部类添加到您的View.IOnTouchListener实现中(这只是一种方法...)
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;
}
}
Now just pass the View to your MyTouchPlusGestures
instance and the gesture and touch listener are assigned for you... 现在,只需将视图传递到
MyTouchPlusGestures
实例,即可为您分配手势和触摸侦听器...
imageView = FindViewById<ImageView>(Resource.Id.background);
touch = new MyTouchPlusGestures(imageView);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.