Android 简明教程

Android - Multitouch

当多个手指同时触摸屏幕时,就会发生多点触控。Android 允许我们检测这些手势。

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

当多个手指同时触摸屏幕时,Android 系统会生成以下触摸事件。

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.

因此,要检测上述任一事件,您需要覆盖 onTouchEvent() 方法并手动检查这些事件。其语法如下所示 −

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;
   }
}

在这些情况下,您可以执行您喜欢的任何计算。例如,缩放、缩小等。要获取 X 轴和 Y 轴的坐标,您可以调用 getX()getY() 方法。其语法如下所示 −

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();

除了这些方法外,此 MotionEvent 类还提供了其他方法,以更好地处理多点触控。这些方法列在下面 −

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 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.

Steps

Description

1

You will use android studio to create an Android application under a package com.example.sairamkrishna.myapplication.

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 application and choose a running android device and install the application on it and verify the results.

以下是修改的主活动文件 src/MainActivity.java 的内容。

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

package com.example.sairamkrishna.myapplication;

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

public 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
         public 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;
         }
      });
   }
}

以下是修改后的 xml res/layout/activity_main.xml 内容。

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

<?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_alignParentTop="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_alignRight="@+id/textview"
      android:layout_alignEnd="@+id/textview"
      android:hint="X-Axis"
      android:layout_alignLeft="@+id/textview"
      android:layout_alignStart="@+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_alignLeft="@+id/editText"
      android:layout_alignStart="@+id/editText"
      android:textColorHint="#ff21ff11"
      android:hint="Y-Axis"
      android:layout_alignRight="@+id/editText"
      android:layout_alignEnd="@+id/editText" />

   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText3"
      android:layout_below="@+id/editText2"
      android:layout_alignLeft="@+id/editText2"
      android:layout_alignStart="@+id/editText2"
      android:hint="Move X"
      android:textColorHint="#ff33ff20"
      android:layout_alignRight="@+id/editText2"
      android:layout_alignEnd="@+id/editText2" />

   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText4"
      android:layout_below="@+id/editText3"
      android:layout_alignLeft="@+id/editText3"
      android:layout_alignStart="@+id/editText3"
      android:textColorHint="#ff31ff07"
      android:hint="Move Y"
      android:layout_alignRight="@+id/editText3"
      android:layout_alignEnd="@+id/editText3" />

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Touch here"
      android:id="@+id/textView2"
      android:layout_alignParentBottom="true"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView"
      android:focusable="true"
      android:typeface="sans"
      android:clickable="true"
      android:textColor="#ff5480ff"
      android:textSize="35dp" />

</RelativeLayout>

以下是 res/values/string.xml 的内容。

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

<resources>
   <string name="app_name">My Application</string>
</resources>

下面是 AndroidManifest.xml 文件的内容。

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.myapplication" >

   <application
      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>

   </application>
</manifest>

让我们尝试运行您的应用程序。我假设您已将您的实际 Android 移动设备连接到您的计算机。要从 Android Studio 运行应用程序,请打开您的一个项目活动文件,然后从工具栏中单击“运行”图标。在启动应用程序之前,Android Studio 将显示以下窗口,让您选择要运行 Android 应用程序的位置。

Let’s try to run your application. 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 click Run icon from the toolbar. Before starting your application, Android studio will display following window to select an option where you want to run your Android application.

choose device

选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 −

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

multitouch

默认情况下,您将在任何字段中看不到任何内容。现在只点击“在此触摸”区域,并在字段中查看一些数据。如下图所示 −

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 −

multitouch1

您会看到移动字段中的数据为 0,因为只执行了单点触控手势。现在轻触屏幕并开始拖动手指。您将看到移动字段数据发生变化。如下图所示 −

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 −

multitouch2