Android:如何创建MotionEvent? [英] Android: How to create a MotionEvent?

查看:116
本文介绍了Android:如何创建MotionEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MotionEvent没有得到一个构造函数,我想在我的单元测试中手动创建一个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 /
    修饰符键的状态事件生成时。



链接到API文档


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 from SystemClock.uptimeMillis().
  • action The kind of action being performed -- one of either ACTION_DOWN, ACTION_MOVE, ACTION_UP, or ACTION_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.

Link to API Docs

这篇关于Android:如何创建MotionEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆