Android中的MotionEvent

来源:互联网 发布:电气工程师绘图软件 编辑:程序博客网 时间:2024/04/29 16:22

译文:

运动事件描述了动作的动作代码和一些列的坐标值。动作代码表明了当触点按下或者弹起等引起的状态变化。坐标值描述了位置信息以及以他的运动属性。

例如,当用户第一次触摸屏幕的时候,系统给窗体发出一个触摸事件,动作代码为ACTION_DOWN,并提供了一些列的坐标值,比如触摸的X、Y坐标,接触区域的压力、尺寸、方向等信息。

一些设备能够在同一时间报告多条运动轨迹。多点触控屏幕为每个手指都发出一条运动轨迹。手指或者其他能够产生运动轨迹的物体都可以叫做触点。运动事件包含所有触点的信息,即使有些触点自从上次事件之后就没有再移动,这些触点必须是当前处于活动状态的。

只有触点按下或者抬起的时候才会影响触点的数量,只有动作取消的时候除外。

每个触点都有一个唯一的id,这个id是在触点第一次按下的时候(动作代码为ACTION_DOWN或者ACTION_POINTER_DOWN)由系统自动分配的。触点的id会一直保持有效,当触点抬起的时候(动作代码为ACTION_UP或者ACTION_POINTER_UP)或者动作取消(动作代码为ACTION_CANCEL)的时候会导致触点的id失效。

MotionEvent类提供了许多可以查看触点的位置或者其他信息的方式,比如getX(int)、getY(int)、getAxisValue(int)、getPointerId(int)、getToolType(int)。这其中的大部分方法都将触点的索引值作为参数而不是触点的id。在事件中,每个触点的索引号的取值范围是从0到getPointerCount()-1。

在一次运动中触点出现的顺序是不确定的。因此,触点的索引值会由于事件的变化而变化,但是只要触点处于活动状态,该触点的id就不会改变。用getPointerId(int)方法可以得到触点的id值,从而根据得到的id值在一连串的动作中来追踪其运动轨迹。然而在连续的运动事件中,应该用findPointerIndex(int)方法通过触点的id值得到触点的索引值。

 

为了提高效率,代码为ACTION_MOVE的运动事件可能会将多个运动路径处理成一个。最常用的当前触点的坐标可以通过getX(int)getY(int)获得。之前的坐标可以通过getHistoricalX(int,int)getHistoricalY(int,int)获得。这些坐标之所以被成为”历史坐标”是因为这些坐标比当前坐标更早的出现了。要想按照时间顺序处理所有坐标,首先要处理历史坐标,然后再是当前坐标。

 

原文:

Motion events describe movements in terms of an action code and a set of axis values. The action code specifies the state change that occurred such as a pointer going down or up. The axis values describe the position and other movement properties.

For example, when the user first touches the screen, the system delivers a touch event to the appropriate View with the action code ACTION_DOWN and a set of axis values that include the X and Y coordinates of the touch and information about the pressure, size and orientation of the contact area.

Some devices can report multiple movement traces at the same time. Multi-touch screens emit one movement trace for each finger. The individual fingers or other objects that generate movement traces are referred to as pointers. Motion events contain information about all of the pointers that are currently active even if some of them have not moved since the last event was delivered.

The number of pointers only ever changes by one as individual pointers go up and down, except when the gesture is canceled.

Each pointer has a unique id that is assigned when it first goes down (indicated by ACTION_DOWN or ACTION_POINTER_DOWN). A pointer id remains valid until the pointer eventually goes up (indicated by ACTION_UP or ACTION_POINTER_UP) or when the gesture is canceled (indicated by ACTION_CANCEL).

The MotionEvent class provides many methods to query the position and other properties of pointers, such as getX(int)getY(int)getAxisValue(int),getPointerId(int)getToolType(int), and many others. Most of these methods accept the pointer index as a parameter rather than the pointer id. The pointer index of each pointer in the event ranges from 0 to one less than the value returned by getPointerCount().

The order in which individual pointers appear within a motion event is undefined. Thus the pointer index of a pointer can change from one event to the next but the pointer id of a pointer is guaranteed to remain constant as long as the pointer remains active. Use the getPointerId(int) method to obtain the pointer id of a pointer to track it across all subsequent motion events in a gesture. Then for successive motion events, use the findPointerIndex(int) method to obtain the pointer index for a given pointer id in that motion event.

For efficiency, motion events with ACTION_MOVE may batch together multiple movement samples within a single object. The most current pointer coordinates are available using getX(int) and getY(int). Earlier coordinates within the batch are accessed using getHistoricalX(int, int) and getHistoricalY(int, int). The coordinates are "historical" only insofar as they are older than the current coordinates in the batch; however, they are still distinct from any other coordinates reported in prior motion events. To process all coordinates in the batch in time order, first consume the historical coordinates then consume the current coordinates.