Android Multitouch Tutorial

Advertisements


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.NoEvent & description
1ACTION_DOWN
For the first pointer that touches the screen. This starts the gesture.
2ACTION_POINTER_DOWN
For extra pointers that enter the screen beyond the first.
3ACTION_MOVE
A change has happened during a press gesture.
4ACTION_POINTER_UP
Sent when a non-primary pointer goes up.
5ACTION_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.NoMethod & description
1getAction()
This method returns the kind of action being performed
2getPressure()
This method returns the current pressure of this event for the first index
3getRawX()
This method returns the original raw X coordinate of this event
4getRawY()
This method returns the original raw Y coordinate of this event
5getSize()
This method returns the size for the first pointer index
6getSource()
This method gets the source of the event
7getXPrecision()
This method return the precision of the X coordinates being reported
8getYPrecision()
This method return the precision of the Y coordinates being reported

Example

Here is an example demonstrating the use of Multitouch. It creates a basic Multitouch gesture application that allows you to view the co-ordinates when multitouch is performed.

To experiment with this example , you need to run this on an actual device.

StepsDescription
1You will use Eclipse IDE to create an Android application and name it as Multitouch under a package com.example.multitouch. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/MainActivity.java file to add multitouch code.
3Modify the res/layout/activity_main to add respective XML components.
4Modify the res/values/string.xml to add necessary string components.
5Run the application and choose a running android device and install the application on it and verify the results.

Following is the content of the modifed main activity file src/com.example.multitouch/MainActivity.java.

package com.example.multitouch;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

   float xAxis = 0f;
   float yAxis = 0f;
   float lastXAxis = 0f;
   float lastYAxis = 0f;
   private EditText xText,yText,moveX,moveY;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      xText = (EditText)findViewById(R.id.editText2);
      yText = (EditText)findViewById(R.id.editText3);
      moveX = (EditText)findViewById(R.id.editText1);
      moveY = (EditText)findViewById(R.id.editText4);
   }
   @Override
   public boolean onTouchEvent(MotionEvent ev){
      final int actionPeformed = ev.getAction();
      switch(actionPeformed){
         case MotionEvent.ACTION_DOWN:{
            final float x = ev.getX();
            final float y = ev.getY();
            lastXAxis = x;
            lastYAxis = y;
            xText.setText(Float.toString(lastXAxis));
            yText.setText(Float.toString(lastYAxis));
         break;
        }
         case MotionEvent.ACTION_MOVE:{
            final float x = ev.getX();
            final float y = ev.getY();
            final float dx = x - lastXAxis;
            final float dy = y - lastYAxis;
            xAxis += dx;
            yAxis += dy;
            moveX.setText(Float.toString(xAxis));
            moveY.setText(Float.toString(yAxis));
         break;
        }
      }
      return true;

   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}

Following is the modified content of the xml res/layout/activity_main.xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <EditText
      android:id="@+id/editText3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editText2"
      android:layout_below="@+id/editText2"
      android:ems="10" />

   <EditText
      android:id="@+id/editText2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_marginTop="25dp"
      android:ems="10" >
      <requestFocus />
   </EditText>

   <EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editText3"
      android:layout_below="@+id/editText3"
      android:ems="10" >
   </EditText>

   <EditText
      android:id="@+id/editText4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editText1"
      android:layout_below="@+id/editText1"
      android:ems="10" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/editText3"
      android:layout_alignParentLeft="true"
      android:text="@string/xaxis"
      android:textAppearance="?android:attr/textAppearanceSmall" />

   <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/editText3"
      android:layout_alignRight="@+id/textView2"
      android:text="@string/yaxis"
      android:textAppearance="?android:attr/textAppearanceSmall" />

   <TextView
      android:id="@+id/textView4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/editText4"
      android:layout_alignLeft="@+id/textView3"
      android:text="@string/MoveX"
      android:textAppearance="?android:attr/textAppearanceSmall" />

   <TextView
      android:id="@+id/textView5"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBaseline="@+id/editText4"
      android:layout_alignBottom="@+id/editText4"
      android:layout_alignRight="@+id/textView4"
      android:text="@string/MoveY"
      android:textAppearance="?android:attr/textAppearanceSmall" />

   <TextView
      android:id="@+id/textView6"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="109dp"
      android:text="@string/perform"
      android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Following is the content of the res/values/string.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">Gestures</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Pinch to zoom in or out!</string>
   <string name="xaxis">X-Axis</string>
   <string name="yaxis">Y-Axis</string>
   <string name="MoveX">Move X</string>
   <string name="MoveY">Move Y</string>
   <string name="perform">Touch here</string>
</resources>

Following is the content of AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.multitouch"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" />

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name="com.example.multitouch.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run your Multitouch application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.

Anroid MediaPlayer Tutorial

Select your mobile device as an option and then check your mobile device which will display your default screen:

Android Multitouch Tutorial

By default you will see nothing in any field. Now just tap on the Touch here area and see some data in the fields. It is shown below:

Android Multitouch Tutorial

You will see that the data in the Move field is 0 , because only a single touch gesture has been performed. Now tap on the screen and start dragging your finger. You will see the change in the data of the move field. It is shown below:

Android Multitouch Tutorial

Advertisements
Advertisements