Multitouch and gesture detection part 2

SDK Version: 

In my previous article I showed how to create a simple class that handles the basic gesture events such as ACTION_DOWN, ACTION_MOVE, ACTION_POINTER_DOWN, ACTION_POINTER_UP, ACTION_UP. Combinations of these, you can implement all of touch gesture stuffs thats you need in your work with a touch screen phone.

Handling multitouch
Create a boolean member variable in your MultitouchView class that stores the actual multitouch event. Name it isMultiTouch.

  1. public class MultitouchView extends View {
  2.         private boolean isMultiTouch = false;
  3.  {...}

The variable's default value is false, because the basic touch event isn't multitouch until the user touches the screen with a second, third non-primary-finger.

In my last tutorial I wrote, the ACTION_POINTER_DOWN action called, when the user touches the screen with a second finger.

  1. case MotionEvent.ACTION_POINTER_DOWN:
  2.                         {
  3.                                 Log.e( "Multitouch", "Action pointer down" );
  4.                                 isMultiTouch =true;
  5.                                 break;
  6.                         }

You have to set the isMultiTouch variable back to false when the ACTION_POINTER_UP action called. The bits in ACTION_POINTER_ID_MASK indicate which pointer changed.

Swipe detection
For swipe detection you have to set a minimal swipe lenght with a member (final) variable.

  1. private static final int        SWIPE_MIN_LENGHT        = 100;

Lesser lenght may cause the system detects swipe gesture instead of point action. Longer lenght may cause the system detects point gesture instead of swipe action. I think 100 is a good choice.

Multitouch detection by time
If you want to detect swipe gesture with multitouch the above example isn't good, because you must touch the screen with the same time and that is hard. I set a minimal time interval thats handles the time between the first and the second finger touch the screen. You have to create long a variable to store the first finger's touch time in ms.

  1. private static final long       MULTITOUCH_DELAY        = 500;
  2. private long    mTouchStart;

500 ms is enough time to touch with the second finger.

  1.         case MotionEvent.ACTION_DOWN:
  2.                         {
  3.                                 mTouchStart = System.currentTimeMillis();
  5.                                 break;
  6.                         }

Set the value when ACTION_DOWN called.
Now calculate with those two variables the multitouch value.

  1.         case MotionEvent.ACTION_POINTER_DOWN:
  2.                         {
  3.                                 Log.e( "Multitouch", "Action pointer down" );
  4.                                 isMultiTouch = (mTouchStart - System.currentTimeMillis()) < MULTITOUCH_DELAY;
  5.                                 break;
  6.                         }

Within a half seconds the application wait for a non primary finger's touch.
That's it, feel free to comment.