How to code for multitouch

后端 未结 2 1968
臣服心动
臣服心动 2020-12-28 10:28

So I\'m developing an application that must handle multitouch. Basically I want single touch for rotating ( this is no problem ). And multitouch for scrolling.

I h

相关标签:
2条回答
  • 2020-12-28 11:06

    You can solve this with simple math, although there may be a better way already built in.

    Just create an object that holds the position of each finger when the .ACTION_POINTER_X_UP event fires and a boolean object that holds which type of touchmode you last used.

    case MotionEvent.ACTION_POINTER_1_UP:
            camera.onTouchDown( xEvent[ 1 ], yEvent[ 1 ] );
            boolean usedMultiTouch = true;
            int x = event.getX(1);
            int y = event.getY(1);
            return true;
    

    Next your ACTION_MOVE (inside the case=1) statement will fire as the user moves their finger as they went from multi to single touch.

    Now, depending on what you want to do, you can either ignore the single move (the boolean is your check) until the .ACTION_UP event fires and you set the boolean back to false

    or

    you call a method that does some math based on the saved location values and the finger that is still on the screen. The math should be pretty easy, but I don't know what you actually want to do.

    0 讨论(0)
  • 2020-12-28 11:07

    Start here if you haven't read it already, it goes over a number of things dealing with multitouch in Android: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html

    A few things about your posted code:

    1. You'll never see ACTION_DOWN with a pointer count of 2. ACTION_DOWN is only sent for the first pointer that goes down. All fingers that touch the screen after the first will send ACTION_POINTER_DOWN.
    2. Don't assume only up to 2 finger touch, there can easily be more.
    3. It's a lot easier to work with the masked action (use MotionEvent#getActionMasked()) than to code for each pointer index individually.
    4. In the end, indices only matter for pulling data out of a MotionEvent. If you're tracking pointer movement over time, use the pointer ID.
    5. Pointer IDs are just numbers. Don't make assumptions as to what values they will have other than that they will be integers from 0 and up.
    0 讨论(0)
提交回复
热议问题