简体   繁体   中英

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)?

Which function should I use? OnFling ? 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. Any ideas?

You can implement the View.IOnTouchListener interface and apply it as a listener to your View ( ImageView in this case):

View.IOnTouchListener implementation:

Note: Using Java.Lang.Object as the base class in this example, but you can use any Java.Lang.Object -based class (Activity, etc...)

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 :

imageView = FindViewById<ImageView>(Resource.Id.background);
touch = new MyTouch();
imageView.SetOnTouchListener(touch);

Update:

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...)

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;
    }
}

Now just pass the View to your MyTouchPlusGestures instance and the gesture and touch listener are assigned for you...

imageView = FindViewById<ImageView>(Resource.Id.background);
touch = new MyTouchPlusGestures(imageView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM