Android的:如何创建一个MotionEvent? [英] Android: How to create a MotionEvent?
问题描述
MotionEvent没有得到一个构造函数,我想在我的单元测试手动创建一个MotionEvent,那么如何获得的?谢谢你。
MotionEvent doesn't get a constructor, I wanted to manually create a MotionEvent in my unit test, then how to get that? Thanks.
推荐答案
您应该使用一个静态获得
的 MotionEvent 方法code>类来创建一个新的事件。
You should use one of the static obtain
methods of the MotionEvent
class to create a new event.
的最简单的方式(除了从现有包装的新事件)是:
The simplest way (besides wrapping a new event from an existing one) is:
static public MotionEvent obtain(long downTime, long eventTime, int action,
float x, float y, int metaState) {
API文档:
创建一个新的MotionEvent,填补了 基本运动值的子集。 那些在这里没有规定是:设备 ID(始终为0),pressure和大小 (始终为1),x和y precision(总是 1),和edgeFlags(始终为0)。
Create a new MotionEvent, filling in a subset of the basic motion values. Those not specified here are: device id (always 0), pressure and size (always 1), x and y precision (always 1), and edgeFlags (always 0).
参数:
-
停机
的时间(毫秒)时, 用户最初$ pssed下来,打开P $ 位置事件流。本 必须从获得 SystemClock.uptimeMillis()。 -
eventTime
的时间(毫秒)时, 生成此特定事件。 这必须从以下地址获得SystemClock.uptimeMillis()
。 -
动作
的动作的类型是 完成 - 一个无论是ACTION_DOWN
,ACTION_MOVE
,ACTION_UP
或ACTION_CANCEL
。 -
X
的X此事件的坐标。 -
是
的y本次活动的协调。 -
亚状态
任何元的状态/ 修改键是为有效 当生成事件。
downTime
The time (in ms) when the user originally pressed down to start a stream of position events. This must be obtained from SystemClock.uptimeMillis().eventTime
The the time (in ms) when this specific event was generated. This must be obtained fromSystemClock.uptimeMillis()
.action
The kind of action being performed -- one of eitherACTION_DOWN
,ACTION_MOVE
,ACTION_UP
, orACTION_CANCEL
.x
The X coordinate of this event.y
The Y coordinate of this event.metaState
The state of any meta / modifier keys that were in effect when the event was generated.
<一个href="http://developer.android.com/reference/android/view/MotionEvent.html#obtain(long,%20long,%20int,%20float,%20float,%20int)">Link到API文档
这篇关于Android的:如何创建一个MotionEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!