MotionEvent doesn't get a constructor, I wanted to manually create a MotionEvent in my unit test, then how to get that? Thanks.
MotionEvent没有构造函数,我想在我的单元测试中手动创建一个MotionEvent,那么如何获得呢?谢谢。
2 个解决方案
#1
81
You should use one of the static obtain
methods of the MotionEvent
class to create a new event.
您应该使用MotionEvent类的一个静态获取方法来创建一个新事件。
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 Docs:
API文档:
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).
创建一个新的MotionEvent,填充基本运动值的子集。这里没有指定的是:设备id(总是0)、压力和大小(总是1)、x和y精度(总是1)和边缘标记(总是0)。
Parameters:
参数:
-
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(). - 停机时间:用户最初按下按钮启动位置事件流的时间(以ms为单位)。这必须从SystemClock.uptimeMillis()获得。
-
eventTime
The the time (in ms) when this specific event was generated. This must be obtained fromSystemClock.uptimeMillis()
. - eventTime:生成此特定事件时的时间(以ms为单位)。这必须从SystemClock.uptimeMillis()获得。
-
action
The kind of action being performed -- one of eitherACTION_DOWN
,ACTION_MOVE
,ACTION_UP
, orACTION_CANCEL
. - 操作正在执行的操作类型——一个是ACTION_DOWN、ACTION_MOVE、ACTION_UP或ACTION_CANCEL。
-
x
The X coordinate of this event. - x是这个事件的x坐标。
-
y
The Y coordinate of this event. - y是这个事件的y坐标。
-
metaState
The state of any meta / modifier keys that were in effect when the event was generated. - 转移事件生成时生效的任何元/修饰符键的状态。
链接到API文档
#2
4
Supplemental answer
Here is an example illustrating the accepted answer:
下面是一个例子,说明了公认的答案:
// get the coordinates of the view
int[] coordinates = new int[2];
myView.getLocationOnScreen(coordinates);
// MotionEvent parameters
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
int action = MotionEvent.ACTION_DOWN;
int x = coordinates[0];
int y = coordinates[1];
int metaState = 0;
// dispatch the event
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);
myView.dispatchTouchEvent(event);
Notes
- Other meta states include things like
KeyEvent.META_SHIFT_ON
, etc. - 其他元状态包括KeyEvent。META_SHIFT_ON等等。
- Thanks to this answer for help with the example.
- 感谢这个示例的帮助。
#1
81
You should use one of the static obtain
methods of the MotionEvent
class to create a new event.
您应该使用MotionEvent类的一个静态获取方法来创建一个新事件。
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 Docs:
API文档:
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).
创建一个新的MotionEvent,填充基本运动值的子集。这里没有指定的是:设备id(总是0)、压力和大小(总是1)、x和y精度(总是1)和边缘标记(总是0)。
Parameters:
参数:
-
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(). - 停机时间:用户最初按下按钮启动位置事件流的时间(以ms为单位)。这必须从SystemClock.uptimeMillis()获得。
-
eventTime
The the time (in ms) when this specific event was generated. This must be obtained fromSystemClock.uptimeMillis()
. - eventTime:生成此特定事件时的时间(以ms为单位)。这必须从SystemClock.uptimeMillis()获得。
-
action
The kind of action being performed -- one of eitherACTION_DOWN
,ACTION_MOVE
,ACTION_UP
, orACTION_CANCEL
. - 操作正在执行的操作类型——一个是ACTION_DOWN、ACTION_MOVE、ACTION_UP或ACTION_CANCEL。
-
x
The X coordinate of this event. - x是这个事件的x坐标。
-
y
The Y coordinate of this event. - y是这个事件的y坐标。
-
metaState
The state of any meta / modifier keys that were in effect when the event was generated. - 转移事件生成时生效的任何元/修饰符键的状态。
链接到API文档
#2
4
Supplemental answer
Here is an example illustrating the accepted answer:
下面是一个例子,说明了公认的答案:
// get the coordinates of the view
int[] coordinates = new int[2];
myView.getLocationOnScreen(coordinates);
// MotionEvent parameters
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
int action = MotionEvent.ACTION_DOWN;
int x = coordinates[0];
int y = coordinates[1];
int metaState = 0;
// dispatch the event
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);
myView.dispatchTouchEvent(event);
Notes
- Other meta states include things like
KeyEvent.META_SHIFT_ON
, etc. - 其他元状态包括KeyEvent。META_SHIFT_ON等等。
- Thanks to this answer for help with the example.
- 感谢这个示例的帮助。