How to code for multitouch

故事扮演 提交于 2019-11-28 19:53:00

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!