Android:如何创建MotionEvent? [英] Android: How to create a MotionEvent?
问题描述
您应该使用静态获取
MotionEvent
类创建一个新的事件。
最简单的方法(除了从现有的一个包装新的事件)是:
static public MotionEvent获取(long downTime,long eventTime,int action,
float x,float y ,int metaState){
API文档:
创建一个新的MotionEvent,填充基本运动值的
子集。
这里没有指定的是:device
id(always 0),压力和大小
(始终为1),x和y精度(总是
1)和edgeFlags 0)。
参数:
-
downTime
用户最初按下开始
a流的位置的时间(以ms为单位)事件。
必须从
SystemClock.uptimeMillis()获取。 -
eventTime
时间(以ms为单位) )当
生成此特定事件。
这必须从
SystemClock.uptimeMillis()
。 -
操作
执行
的一种操作 -
ACTION_DOWN
,ACTION_MOVE
,
ACTION_UP
或ACTION_CANCEL
。 -
x
此事件的X坐标。 -
y
Y -
metaState
有效
的任何meta /
修饰符键的状态事件生成时。
MotionEvent doesn't get a constructor, I wanted to manually create a MotionEvent in my unit test, then how to get that? Thanks.
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 Docs:
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).
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().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屋!