Multitouch Gesture in Android

Multi-touch gesture happens when more then one finger touches the screen at the same time. Android allows us to detect these gestures.

Android system generates the following touch events whenever multiple fingers touches the screen at the same time.

Sr.No Event & description
1

ACTION_DOWN

For the first pointer that touches the screen. This starts the gesture.

2

ACTION_POINTER_DOWN

For extra pointers that enter the screen beyond the first.

3

ACTION_MOVE

A change has happened during a press gesture.

4

ACTION_POINTER_UP

Sent when a non-primary pointer goes up.

5

ACTION_UP

Sent when the last pointer leaves the screen.

So in order to detect any of the above mention event , you need to override onTouchEvent() method and check these events manually. Its syntax is given below −

public boolean onTouchEvent(MotionEvent ev){
   final int actionPeformed = ev.getAction();

   switch(actionPeformed){
      case MotionEvent.ACTION_DOWN:{
         break;
      }
   
      case MotionEvent.ACTION_MOVE:{
         break;
      }
      return true;
   }
}

In these cases, you can perform any calculation you like. For example zooming , shrinking e.t.c. In order to get the co-ordinates of the X and Y axis, you can call getX() and getY() method. Its syntax is given below −

final float x = ev.getX();
final float y = ev.getY();

Apart from these methods, there are other methods provided by this MotionEvent class for better dealing with multitouch. These methods are listed below −

Sr.No Method & description
1

getAction()

This method returns the kind of action being performed

2

getPressure()

This method returns the current pressure of this event for the first index

3

getRawX()

This method returns the original raw X coordinate of this event

4

getRawY()

This method returns the original raw Y coordinate of this event

5

getSize()

This method returns the size for the first pointer index

6

getSource()

This method gets the source of the event

7

getXPrecision()

This method return the precision of the X coordinates being reported

8

getYPrecision()

This method return the precision of the Y coordinates being reporte