English 中文(简体)
Android Basics

Android - User Interface

Android Advanced Concepts

Android Useful Examples

Android Useful Resources

Selected Reading

Android - Multitouch
  • 时间:2024-11-03

Android - Multitouch


Previous Page Next Page  

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 −

pubpc 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 pke. 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 deapng with multitouch. These methods are psted 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 reported

Example

Here is an example demonstrating the use of Multitouch. It creates a basic Multitouch gesture apppcation 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.

Steps Description
1 You will use android studio to create an Android apppcation under a package com.example.sairamkrishna.myapppcation.
2 Modify src/MainActivity.java file to add multitouch code.
3 Modify the res/layout/activity_main to add respective XML components.
4 Run the apppcation and choose a running android device and install the apppcation on it and verify the results.

Following is the content of the modified main activity file src/MainActivity.java.

package com.example.sairamkrishna.myapppcation;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

pubpc class MainActivity extends Activity {
   float xAxis = 0f;
   float yAxis = 0f;

   float lastXAxis = 0f;
   float lastYAxis = 0f;

   EditText ed1, ed2, ed3, ed4;
   TextView tv1;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ed1 = (EditText) findViewById(R.id.editText);
      ed2 = (EditText) findViewById(R.id.editText2);
      ed3 = (EditText) findViewById(R.id.editText3);
      ed4 = (EditText) findViewById(R.id.editText4);

      tv1=(TextView)findViewById(R.id.textView2);
		
      tv1.setOnTouchListener(new View.OnTouchListener() {
         @Override
         pubpc boolean onTouch(View v, MotionEvent event) {
            final int actionPeformed = event.getAction();

            switch(actionPeformed){
               case MotionEvent.ACTION_DOWN:{
                  final float x = event.getX();
                  final float y = event.getY();

                  lastXAxis = x;
                  lastYAxis = y;

                  ed1.setText(Float.toString(lastXAxis));
                  ed2.setText(Float.toString(lastYAxis));
                  break;
               }

               case MotionEvent.ACTION_MOVE:{
                  final float x = event.getX();
                  final float y = event.getY();

                  final float dx = x - lastXAxis;
                  final float dy = y - lastYAxis;

                  xAxis += dx;
                  yAxis += dy;

                  ed3.setText(Float.toString(xAxis));
                  ed4.setText(Float.toString(yAxis));
                  break;
               }
            }
            return true;
         }
      });
   }
}

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

In the below code abcindicates the logo of tutorialspoint.com
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:transitionGroup="true">
   
   <TextView android:text="Multitouch example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_apgnParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:theme="@style/Base.TextAppearance.AppCompat" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_below="@+id/imageView"
      android:layout_apgnRight="@+id/textview"
      android:layout_apgnEnd="@+id/textview"
      android:hint="X-Axis"
      android:layout_apgnLeft="@+id/textview"
      android:layout_apgnStart="@+id/textview"
      android:textColorHint="#ff69ff0e" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_below="@+id/editText"
      android:layout_apgnLeft="@+id/editText"
      android:layout_apgnStart="@+id/editText"
      android:textColorHint="#ff21ff11"
      android:hint="Y-Axis"
      android:layout_apgnRight="@+id/editText"
      android:layout_apgnEnd="@+id/editText" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText3"
      android:layout_below="@+id/editText2"
      android:layout_apgnLeft="@+id/editText2"
      android:layout_apgnStart="@+id/editText2"
      android:hint="Move X"
      android:textColorHint="#ff33ff20"
      android:layout_apgnRight="@+id/editText2"
      android:layout_apgnEnd="@+id/editText2" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText4"
      android:layout_below="@+id/editText3"
      android:layout_apgnLeft="@+id/editText3"
      android:layout_apgnStart="@+id/editText3"
      android:textColorHint="#ff31ff07"
      android:hint="Move Y"
      android:layout_apgnRight="@+id/editText3"
      android:layout_apgnEnd="@+id/editText3" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Touch here"
      android:id="@+id/textView2"
      android:layout_apgnParentBottom="true"
      android:layout_apgnLeft="@+id/imageView"
      android:layout_apgnStart="@+id/imageView"
      android:focusable="true"
      android:typeface="sans"
      android:cpckable="true"
      android:textColor="#ff5480ff"
      android:textSize="35dp" />

</RelativeLayout>

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

<resources>
   <string name="app_name">My Apppcation</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.sairamkrishna.myapppcation" >

   <apppcation
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".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>
   
   </apppcation>
</manifest>

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

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