Android 简明教程
Android - Gestures
Android 提供了特殊类型的触摸屏事件,例如捏、双击、滚动、长按和抽动。这些统称为手势。
Android provides special types of touch screen events such as pinch , double tap, scrolls , long presses and flinch. These are all known as gestures.
Android 提供了 GestureDetector 类来接收运动事件,并告诉我们这些事件是否与手势对应。要使用它,您需要创建一个 GestureDetector 对象,然后使用 GestureDetector.SimpleOnGestureListener 扩展另一个类作为侦听器并覆盖一些方法。其语法如下所述:
Android provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not. To use it , you need to create an object of GestureDetector and then extend another class with GestureDetector.SimpleOnGestureListener to act as a listener and override some methods. Its syntax is given below −
GestureDetector myG;
myG = new GestureDetector(this,new Gesture());
class Gesture extends GestureDetector.SimpleOnGestureListener{
public boolean onSingleTapUp(MotionEvent ev) {
}
public void onLongPress(MotionEvent ev) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
}
}
Handling Pinch Gesture
Android 提供了 ScaleGestureDetector 类来处理捏等手势。为了使用它,您需要实例化此类的对象。其语法如下:
Android provides ScaleGestureDetector class to handle gestures like pinch e.t.c. In order to use it, you need to instantiate an object of this class. Its syntax is as follow −
ScaleGestureDetector SGD;
SGD = new ScaleGestureDetector(this,new ScaleListener());
第一个参数是上下文,第二个参数是事件侦听器。我们必须定义事件侦听器并覆盖函数 OnTouchEvent 以使其工作。其语法如下所述:
The first parameter is the context and the second parameter is the event listener. We have to define the event listener and override a function OnTouchEvent to make it working. Its syntax is given below −
public boolean onTouchEvent(MotionEvent ev) {
SGD.onTouchEvent(ev);
return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale = detector.getScaleFactor();
return true;
}
}
除了捏手势之外,还有其他可用的方法来通知更多有关触摸事件的信息。它们如下所列:
Apart from the pinch gestures , there are other methods available that notify more about touch events. They are listed below −
Sr.No |
Method & description |
1 |
getEventTime() This method get the event time of the current event being processed.. |
2 |
getFocusX() This method get the X coordinate of the current gesture’s focal point. |
3 |
getFocusY() This method get the Y coordinate of the current gesture’s focal point. |
4 |
getTimeDelta() This method return the time difference in milliseconds between the previous accepted scaling event and the current scaling event. |
5 |
isInProgress() This method returns true if a scale gesture is in progress.. |
6 |
onTouchEvent(MotionEvent event) This method accepts MotionEvents and dispatches events when appropriate. |
Example
以下示例演示如何使用 ScaleGestureDetector 类。它创建了一个基本应用程序,允许您通过捏合来放大和缩小。
Here is an example demonstrating the use of ScaleGestureDetector class. It creates a basic application that allows you to zoom in and out through pinch.
若要对该示例进行试验,您可在实际设备上或在启用了触摸屏的模拟器中运行该示例。
To experiment with this example , you can run this on an actual device or in an emulator with touch screen enabled.
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 necessary 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.graphics.Matrix;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
private Matrix matrix = new Matrix();
private float scale = 1f;
private ScaleGestureDetector SGD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.imageView);
SGD = new ScaleGestureDetector(this,new ScaleListener());
}
public boolean onTouchEvent(MotionEvent ev) {
SGD.onTouchEvent(ev);
return true;
}
private class ScaleListener extends ScaleGestureDetector.
SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scale *= detector.getScaleFactor();
scale = Math.max(0.1f, Math.min(scale, 5.0f));
matrix.setScale(scale, scale);
iv.setImageMatrix(matrix);
return true;
}
}
}
以下是修改后的 xml res/layout/activity_main.xml 内容。
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: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" >
<TextView android:text="Gestures
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:scaleType="matrix"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</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="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sairamkrishna.myapplicationMainActivity"
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 运行该应用,打开项目的某个活动文件并从工具栏中单击“运行”图标。示例输出应如下所示:
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.The sample output should be like this −
现在,只需将两个手指放在 Android 屏幕上,并将它们分开,您会看到 android 图像正在放大。如下面的图像所示:
Now just place two fingers over android screen , and separate them a part and you will see that the android image is zooming. It is shown in the image below −
现在,再次将两个手指放在 Android 屏幕上,尝试合拢它们,您会看到 android 图像正在缩小。如下面的图像所示:
Now again place two fingers over android screen, and try to close them and you will see that the android image is now shrinking. It is shown in the image below −