这两天在做模块的单元测试,需要模拟触屏事件,手势操作,下面针对MotionEvent做下代码记录:

下面的事件注入都会调用一个函数:

Instrumentation mInstrumentation;public void sendPointerEvent(int action, Point point) {MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), action, point.x, point.y, 0);mInstrumentation.sendPointerSync(event);event.recycle();}

1,MotionEvent.ACTION_DOWN

Down跟UP通常是连着的。

Point selectPoint = new Point(670,470);
sendPointerEvent(MotionEvent.ACTION_DOWN, selectPoint);
sendPointerEvent(MotionEvent.ACTION_UP, selectPoint);

2,MotionEvent.ACTION_MOVE

从一个Down事件开始,中间loop多个Move,最后一个Up事件。

Point from = new Point(150, 650);
Point to = new Point(150, 260);
sendPointerEvent(MotionEvent.ACTION_DOWN, from);
MovePointerEvent(from, to, 10, 10);
sendPointerEvent(MotionEvent.ACTION_UP, to);

这里计算move的步进:

    private int getNextMoveValue(int oldValue, int targetValue, int distance, int step) {if (targetValue - oldValue > distance) {return oldValue + step;} else if (targetValue - oldValue < -distance) {return oldValue - step;} else {return targetValue;}}

3,手势操作,缩放

private int PINCH_STEP_COUNT = 10;public void scaleGestureDetector() {try {final float initialScaleFactor = 1.0f;final DisplayMetrics dm = new DisplayMetrics();final WindowManager wm =(WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);//obtain the display size ,so that to get start and end coordinates.wm.getDefaultDisplay().getMetrics(dm);final int displayWidth = dm.widthPixels;final int displayHeight = dm.heightPixels;//from center,pinch to 75% of display.final int centerX = displayWidth / 2;final int centerY = displayHeight / 2;final float[] firstFingerStartCoords = new float[] {centerX + 1.0f, centerY - 1.0f};final float[] firstFingerEndCoords =new float[] {0.75f * displayWidth, 0.25f * displayHeight};//the second point different from the first.final float[] secondFingerStartCoords = new float[] {centerX - 1.0f, centerY + 1.0f};final float[] secondFingerEndCoords =new float[] {0.25f * displayWidth, 0.75f * displayHeight};//specify the touch properites.final MotionEvent.PointerProperties pp1 = new MotionEvent.PointerProperties();pp1.id = 0;pp1.toolType = MotionEvent.TOOL_TYPE_FINGER;final MotionEvent.PointerProperties pp2 = new MotionEvent.PointerProperties();pp2.id = 1;pp2.toolType = MotionEvent.TOOL_TYPE_FINGER;MotionEvent.PointerProperties[] pointerProperties =new MotionEvent.PointerProperties[]{pp1, pp2};//specify the motion properites.final MotionEvent.PointerCoords pc1 = new MotionEvent.PointerCoords();pc1.x = firstFingerStartCoords[0];pc1.y = firstFingerStartCoords[1];pc1.pressure = 1;pc1.size = 1;final MotionEvent.PointerCoords pc2 = new MotionEvent.PointerCoords();pc2.x = secondFingerStartCoords[0];pc2.y = secondFingerEndCoords[1];pc2.pressure = 1;pc2.size = 1;final long startTime = SystemClock.uptimeMillis();long eventTime = startTime;final MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[]{pc1, pc2};final MotionEvent firstFingerEvent = MotionEvent.obtain(startTime,eventTime, MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords,0, 0, 1, 1, 0, 0, 0, 0);eventTime = SystemClock.uptimeMillis();final MotionEvent secondFingerEvent = MotionEvent.obtain(startTime, eventTime,MotionEvent.ACTION_POINTER_DOWN +(pp2.id << MotionEvent.ACTION_POINTER_INDEX_SHIFT),2, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0);//inject the start down event.mInstrumentation.sendPointerSync(firstFingerEvent);mInstrumentation.sendPointerSync(secondFingerEvent);final float[][] stepsFirstFinger = interpolate(firstFingerStartCoords,firstFingerEndCoords);final float[][] stepsSecondFinger = interpolate(secondFingerStartCoords,secondFingerEndCoords);//loop the motion of two fingers to the end points.for (int i = 0; i < PINCH_STEP_COUNT; i++) {eventTime = SystemClock.uptimeMillis();pc1.x = stepsFirstFinger[i][0];pc1.y = stepsFirstFinger[i][1];pc2.x = stepsSecondFinger[i][0];pc2.y = stepsSecondFinger[i][1];final MotionEvent event = MotionEvent.obtain(startTime, eventTime,MotionEvent.ACTION_MOVE, 2, pointerProperties, pointerCoords,0, 0, 1, 1, 0, 0, 0, 0);mInstrumentation.sendPointerSync(event);Thread.sleep(TIMEWAITING*4/5);}eventTime = SystemClock.uptimeMillis();final MotionEvent secondFingerUpEvent = MotionEvent.obtain(startTime, eventTime,MotionEvent.ACTION_POINTER_UP, 2, pointerProperties, pointerCoords,0, 0, 1, 1, 0, 0, 0, 0);//inject the up event.mInstrumentation.sendPointerSync(secondFingerUpEvent);eventTime = SystemClock.uptimeMillis();final MotionEvent firstFingerUpEvent = MotionEvent.obtain(startTime, eventTime,MotionEvent.ACTION_POINTER_UP, 1, pointerProperties, pointerCoords,0, 0, 1, 1, 0, 0, 0, 0);mInstrumentation.sendPointerSync(firstFingerUpEvent);}catch (InterruptedException e) {e.printStackTrace();Thread.currentThread().interrupt();}}

两个手指移动的差值计算:

    private float[][] interpolate(float[] start, float[] end) {float[][] res = new float[PINCH_STEP_COUNT][2];for (int i = 0; i < PINCH_STEP_COUNT; i++) {res[i][0] = start[0] + (end[0] - start[0]) * i / (PINCH_STEP_COUNT - 1f);res[i][1] = start[1] + (end[1] - start[1]) * i / (PINCH_STEP_COUNT - 1f);}return res;}

模拟MotionEvent事件相关推荐

  1. Android : 模拟点击performClick()/模拟长按performLongClick()/模拟onTouch事件

    参考 Android 模拟触摸动作MotionEvent事件 android MotionEvent.obtain模拟事件,自动触发 一.模拟点击事件: mBtn1.performClick(); / ...

  2. 移动端为何不使用click而模拟tap事件及解决方案

    移动端为何不使用click而模拟tap事件及解决方案 参考文章: (1)移动端为何不使用click而模拟tap事件及解决方案 (2)https://www.cnblogs.com/ranyonsue/ ...

  3. Qt文档阅读笔记-Qt单元测试中模拟GUI事件

    这里先提下基本概念 官方的模拟GUI事件,就是调用QTest提供的函数,去模拟用户鼠标点击,用户输入等效果.从而达到模拟的功能,感觉这个功能非常的好. 如: QTest::keyClicks()主要是 ...

  4. uinput 用法 android 上层使用uinput 的用法来模拟 input 事件

    android 上层使用uinput 的用法来模拟 input 事件 #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif#include <stdio. ...

  5. MotionEvent事件在onInterceptTouchEvent()、onTouchEvent()中的传递顺序

    onInterceptTouchEvent()用于处理事件并改变事件的传递方向.处理事件这个不用说了,你在函数内部编写代码处理就可以了.而决定传递方向的是返回值,返回为false时事件会传递给子控件的 ...

  6. java鼠标事件_Java 模拟鼠标事件

    导读热词 下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. @H_502_5@/** * Clicks in a given area of ...

  7. 【键盘】jQuery+CSS3模拟键盘事件(精)

    jQuery+CSS3模拟键盘事件是一款基于jQuery的模拟键盘事件的应用,键盘上除了功能键,其他键都可以用这款插件来模拟.当你敲击键盘上的键时,这款应用会帮助你获取到这个键的事件,并在页面上的模拟 ...

  8. selenium 模拟键盘事件 复制粘贴、右键、回车等

    [selenium 模拟键盘事件 复制粘贴.右键.回车等] #coding=utf-8 ''' selenium ''' from selenium import webdriver as wd im ...

  9. C#使用mouse_event函数模拟鼠标事件

    C#使用mouse_event函数模拟鼠标事件 mouse_event函数 private static extern int mouse_event(int dwFlags, int dx, int ...

最新文章

  1. api 原生hbase_HBase客户端API
  2. 大牛深入浅出讲解c语言do{...}while(0)功能及用法
  3. 《大型门户网站是这样炼成的!(Struts 2+Spring 2+Hibernate 3) 》
  4. 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262
  5. 在ASP.NET CORE 2.0使用SignalR技术
  6. matplotlib 横坐标少了一个点_收藏起来!比 matplotlib 效率高十倍的数据可视化神器!...
  7. 解决自定义UITableViewCell在浏览中出现数据行重复的问题
  8. unity linux桌面环境,现在仍然可以在Ubuntu 20.04上安装Unity桌面环境
  9. 【Linux】修改镜像源
  10. Fluent 安装过程
  11. Linux下常见音频格式之间的转换方法【转】
  12. 如何在线将flac格式转换成mp3音频
  13. “出神”才能提高创造力——史蒂芬 · 科特勒 杰米 · 威尔
  14. Springboot 通过Jedis-clients 操作Redis
  15. 【H5】用易企秀做H5做完发现页面播放顺序乱套了???请不要单选自动播放
  16. 首次使用阿帕奇下的ab测压工具测试程序
  17. 如何通过QQ邮箱获取授权码
  18. Windows Mobil中解决日期显示不正常的方法
  19. 【Cxinny】数据结构与算法
  20. 【HTTP】如何避免OPTIONS请求?

热门文章

  1. ChatGPT-4震撼发布
  2. 用Python爬取中国各省GDP数据
  3. FTP服务器架设--安全篇(转)
  4. JS中click和onclick本质区别
  5. drwtsn32.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉
  6. android桌面文件夹美化
  7. 用主题模型可视化分析911新闻(Python版)
  8. 对话机器人(七)——RASA:基于规则
  9. 如何练习好一个技能?
  10. pycharm写代码突然乱码 出现花里胡哨的字符 繁体字等