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
类的静态 obtain
方法之一来创建新事件.
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),压力和大小(总是 1),x 和 y 精度(总是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).
参数:
downTime
时间(以毫秒为单位)用户最初按下启动位置事件流.这必须从SystemClock.uptimeMillis().eventTime
时间(以毫秒为单位)这个特定的事件产生了.这必须从SystemClock.uptimeMillis()
.action
正在执行的操作类型执行——其中之一ACTION_DOWN
,ACTION_MOVE
,ACTION_UP
或ACTION_CANCEL
.x
此事件的 X 坐标.y
此事件的 Y 坐标.metaState
任何元的状态/有效的修饰键事件发生的时间.
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.
这篇关于Android:如何创建 MotionEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!