android触摸手势
Mobile users interact with apps in many ways, a major one is through touch. Following motion guidelines and supporting common touch gestures goes a long way toward providing an intuitive experience to your users.
移动用户通过多种方式与应用进行交互,其中主要的一种是通过触摸。 遵循运动准则并支持常见的触摸手势在为用户提供直观体验方面大有帮助。
This article goes through the basics of touch gestures in Android, including single touch gestures, multi-touch gestures and some out-of-the-box common gestures Android supports. Along the way, you’ll see how these concepts are used to build a simple drawing app.
本文介绍了Android中触摸手势的基础知识,包括单点触摸手势,多点触摸手势以及Android支持的一些现成的常用手势。 一路上,您将看到如何使用这些概念来构建简单的绘图应用程序。
触控手势 (Touch gestures)
As a user touches a View, the system sends signals to the View with information about each touch event, including its nature and where exactly it occurred. This allows you to track the user’s touch interactions with the View, and potentially detect gestures.
当用户触摸View ,系统会将有关每个触摸事件的信息(包括其性质以及发生的确切位置)发送到View 。 这使您可以使用View跟踪用户的触摸交互,并可能检测手势。
The term “pointer” is used to refer to the finger or object (e.g. stylus) that interacts with the View. Therefor, a touch event may be the result of 1 or multiple pointers. When the Android device’s screen supports multi-touch, the system can track and report information about all the pointers as they interact with the View.
术语“指针”用于指代与View交互的手指或物体(例如,手写笔)。 因此,触摸事件可能是1个或多个指针的结果。 当Android设备的屏幕支持多点触摸时,系统可以在与View交互时跟踪和报告有关所有指针的信息。
To track touch events occurring on a View, set its OnTouchListener.
要跟踪View上发生的触摸事件,请设置其OnTouchListener 。
跟踪单触手势 (Tracking Single Touch Gestures)
When dealing with single touch gestures, handle each event using its type and coordinates.
处理单点触摸手势时,请使用事件的类型和坐标来处理每个事件。
The event’s type is available through MotionEvent.getAction(). It returns an integer whose value can be one of the following: ACTION_DOWN, ACTION_UP, ACTION_MOVE and ACTION_CANCEL, which signal that a new touch gesture has started, finished, moved or been cancelled respectively.
可通过MotionEvent.getAction()获得事件的类型。 它返回一个整数,其值可以为以下值之一: ACTION_DOWN , ACTION_UP , ACTION_MOVE和ACTION_CANCEL ,分别表示新的触摸手势已开始,完成,移动或取消。
The event’s coordinates are represented by a pair of x and y coordinates where the touch event occurred. The coordinates are relative to the View and not the whole screen, meaning that a touch event at the upper left corner of the View will have the coordinates (0, 0). The coordinates are decimals when the device has sub-pixel precision, and are otherwise integers.
事件的坐标由发生触摸事件的一对x和y坐标表示。 坐标是相对于View而不是整个屏幕的,这意味着View左上角的触摸事件将具有坐标(0,0) 。 当设备具有亚像素精度时,坐标为小数,否则为整数。
A set of consecutive events between ACTION_DOWN and ACTION_UP represent a gesture, for example, a scroll gesture starts with the user’s finger touching the screen (ACTION_DOWN), it then moves up the screen (ACTION_MOVE), before finally stopping and being lifted off the screen (ACTION_UP).
ACTION_DOWN和ACTION_UP之间的一组连续事件表示一个手势,例如,滚动手势从用户的手指触摸屏幕开始( ACTION_DOWN ),然后向上移动屏幕( ACTION_MOVE ),最后停止并抬离屏幕( ACTION_UP )。
Single Touch Gestures Example
单点触控手势示例
The following sample tracks single touch events on a View and allows the user to draw on it. The drawing follows the user’s finger (pointer).
以下示例跟踪View上的单点触摸事件,并允许用户在其上进行绘制。 该图跟随用户的手指(指针)。
跟踪多点触控手势 (Tracking Multi-Touch Gestures)
When dealing with multi-touch gestures, you may have more than a single pointer touching a View, the system provides information about each of the pointers and tracks them during a gesture, from the moment the first pointer touches the View until the last pointer is lifted off the View.
当使用多点触摸手势打交道时,你可能有不止一个指针触摸View ,系统手势时提供关于每个指针和轨道他们的信息,从目前看, 第一指示体触摸View ,直到最后指针解除View 。
Some of the information you’ll use for multi-touch events includes the event’s type, pointer id, pointer index and coordinates.
您将用于多点触摸事件的一些信息包括事件的类型,指针ID,指针索引和坐标。
The event’s type is provided by MotionEvent.getActionMasked(). It returns an integer whose value can be one of the following: ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE, ACTION_POINTER_UP, ACTION_UP , which respectively signal that the first pointer has touched the View and the gesture has started, a new pointer has touched the View, a pointer has moved, a pointer has been lifted and the last pointer has been lifted.
事件的类型由MotionEvent.getActionMasked() 。 它返回一个整数,其值可以是以下值之一: ACTION_DOWN , ACTION_POINTER_DOWN , ACTION_MOVE , ACTION_POINTER_UP , ACTION_UP ,分别指示第一个指针触摸了View且手势已开始,新指针触摸了View ,一个指针已移动,指针已提起, 最后一个指针已提起。
The event may have multiple pointers, each pointer is identified by an id and an index, the id is unique throughout the entire gesture, whereas the index is unique per event, but may change as pointers go up and down throughout the gesture. As a result, a pointer’s index makes sense and should be used in the context of a multi-touch event, since it will always be 0 in a single touch event. You can get a pointer’s id using MotionEvent.getPointerId(), and you can get its index using MotionEvent.getActionIndex(). The value of the pointer index will always be between 0 -inclusive- and MotionEvent.getPointerCounter() -exclusive-.
该事件可能具有多个指针,每个指针由一个ID和一个索引标识,该ID在整个手势中都是唯一的,而索引在每个事件中都是唯一的,但是随着指针在整个手势中的上下移动,它可能会发生变化。 结果,指针索引很有意义,应该在多点触摸事件的上下文中使用,因为在单点触摸事件中索引始终为0。 您可以使用得到一个指针的id MotionEvent.getPointerId()你可以用得到它的指数MotionEvent.getActionIndex() 指针索引的值将始终在0 -inclusive-和MotionEvent.getPointerCounter MotionEvent.getPointerCounter() exclusive-之间。
Multi-Touch Gestures Example
多点触控手势示例
The following sample tracks multi-touch events on a View and allows the user to draw on it. The drawings follow the user’s fingers (pointers).
以下示例跟踪View上的多点触摸事件,并允许用户在其上进行绘制。 绘图遵循用户的手指(指针)。
检测手势 (Detecting Gestures)
Detecting gestures on a View such as when it’s long pressed and scrolled is accomplished by analyzing a set of consecutive touch events received from the View’s OnTouchListener.
通过分析从View的OnTouchListener接收到的一组连续的触摸事件,可以检测到View上的手势(例如长按和滚动时)。
Detecting Common Gestures
检测常见手势
Android supports detecting certain common gestures including single tap, double tap, long press, scroll and fling with GestureDetector. Implement an OnGestureListener, attach it to a GestureDetector and use it inside the View’s OnTouchListener by passing it each touch event the View receives. If the GestureDetector detects any of the gestures is supports, it will trigger its appropriate callback.
Android支持使用GestureDetector检测某些常见手势,包括单击,双击,长按,滚动和GestureDetector 。 实现一个OnGestureListener ,将其附加到GestureDetector并通过将View接收到的每个触摸事件传递给它,在View's OnTouchListener使用它。 如果GestureDetector检测到任何手势GestureDetector支持,它将触发其适当的回调。
To use GestureDetector’s OnGestureListener, you must implement all its gesture callbacks. If you only need to detect a subset of these gestures, use a SimpleOnGestureListener instead.
要使用GestureDetector的OnGestureListener ,必须实现其所有手势回调。 如果只需要检测这些手势的子集,请改用SimpleOnGestureListener 。
Common Gestures Example
常见手势示例
The following sample tracks double taps on a View and allows the user to clear any drawings it may contain.
以下示例在View上跟踪双击,并允许用户清除其可能包含的所有图形。
Detecting Scale Gestures
检测刻度手势
Another common gesture on Android is the scale gesture, where using 2 fingers, you pinch or reverse pinch a View. This gesture is used for example in camera apps to zoom in and out of the camera preview, or in image gallery apps to zoom in and out of a photo.
Android上的另一种常见手势是比例手势,您可以使用两根手指捏或反捏View 。 例如,此手势用于相机应用程序以放大和缩小相机预览,或在图库应用程序中使用此手势放大和缩小照片。
To detect a scale gesture, follow the same pattern as above, implement an OnScaleGestureListener, attach it to a ScaleGestureDetector and pass it each incoming touch event from the View’s OnTouchListener.
要检测缩放手势,请遵循与上述相同的模式,实现OnScaleGestureListener ,将其附加到ScaleGestureDetector并从View的OnTouchListener传入每个传入的触摸事件。
To use ScaleGestureDetector’s OnScaleGestureListener, you must implement all its callbacks, if you’re only interested in getting the factor by which the scale has changed, use instead SimpleOnScaleGestureListener.
要使用ScaleGestureDetector的OnScaleGestureListener ,您必须实现其所有回调,如果您仅对获取比例改变的因素感兴趣,请改用SimpleOnScaleGestureListener 。
Scale Gestures Example
比例手势示例
The following sample tracks scale gestures on a View, so that when the user pinches it, the View zooms in, and when they reverse pinch it, the View zooms out.
以下示例跟踪View上的缩放手势,以便当用户捏住手势时, View放大,而当用户捏住手势时, View缩小。
触摸手势代码示例 (Touch Gestures Code Sample)
The code snippets used in this articles were taken from the following repo.
本文中使用的代码段摘自以下回购。
结论 (Conclusion)
In summary:
综上所述:
-
Track a
View’s touch events and gestures using itsOnTouchListener.使用其
OnTouchListener跟踪View的触摸事件和手势。 - Track a single touch gesture by using the type of its touch events, and the coordinates of its single pointer. 通过使用其触摸事件的类型及其单个指针的坐标来跟踪单个触摸手势。
- Track a multi-touch gesture by using the types of its touch events, their pointer ids, pointer indices, and their coordinates. 通过使用其触摸事件的类型,其指针ID,指针索引及其坐标来跟踪多点触摸手势。
-
Detect common gestures like single and double taps, long presses, scrolling and flings using
GestureDetector. Implement its listener and attach it to aView’sOnTouchListener.使用
GestureDetector检测常见的手势,例如GestureDetector和双击,长按,滚动和GestureDetector。 实现其侦听器,并将其附加到View的OnTouchListener。 -
Detect scale gestures using
ScaleGestureDetector. Implement its listener and attach it to aView’sOnTouchListener.使用
ScaleGestureDetector检测比例手势。 实现其侦听器,并将其附加到View的OnTouchListener。
For more on touch gestures, check out the following:
有关触摸手势的更多信息,请查看以下内容:
翻译自: https://proandroiddev.com/detecting-touch-gestures-in-android-f8eb4a4faf98
android触摸手势