android 模拟发送多点触摸事件

简介: android 模拟发送多点触摸事件

android的输入事件处理, 大多跟InputReader, InputManager, InputManagerService有关,


对它们的理解,也只是皮毛.


本文也只涉及到Touch事件的DOWN, MOVE, UP;


如何发送或虚拟Touch事件?多点触摸的时候又应该如何处理?



注: 本文中的代码对于第三方应用来说可能不适用!


//用于发送事件
        public void postMotionEvent(final InputManager imgr, final MotionEvent event, int delayed){
  h.postDelayed(new Runnable(){
    public void run(){
    event.setSource(InputDevice.SOURCE_TOUCHSCREEN);//留意参数
    imgr.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);//同样, 留意参数, 如果参数错, 会不成功.
    }
  }, delayed);
  }
  /** double point ***********/
        //用于模拟两点缩放事件.
  /** AnsonCode 2013.4.27 **/
  public void postScale(boolean zoomIn, Context cxt){
//计算坐标, 我选取的是屏幕中间的区域.
  android.content.res.Resources res = cxt.getResources();
  int width = res.getDisplayMetrics().widthPixels;
  int height = res.getDisplayMetrics().heightPixels;
  long downTime = SystemClock.uptimeMillis();
  InputManager imgr = InputManager.getInstance();//APK开发可能访问不到.
  int centerx = width /2;
  int centery = height  /2;
  int x1 = centerx - 100, y1 = centery - 100;
  int x2 = centerx + 100, y2 = centery + 100;
  part 1
  PointerCoords[] coords = new PointerCoords[2];//点1坐标.
  PointerCoords pointerCoords = new PointerCoords();
  pointerCoords.x = x1;
  pointerCoords.y = y1;
  coords[0] = pointerCoords;
  MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
            1, new int[]{0}, coords, 0, 0, 0, 0, 0, 0, 0);//创建点1的DOWN事件
  postMotionEvent(imgr, event, 0);//点1 DOWN了
  pointerCoords = new PointerCoords();//创建点2
  pointerCoords.x = x2;
  pointerCoords.y = y2;
  coords[1] = pointerCoords;
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_POINTER_2_DOWN,
            2, new int[]{0,1}, coords, 0, 0, 0, 0, 0, 0, 0);//点2 DOWN 事件
  postMotionEvent(imgr, event, 10);//点2也 DOWN 了
  ///part 2
                //第2部分主要是 MOVE 事件.
  pointerCoords = new PointerCoords();
  pointerCoords.x = x1 - (zoomIn ? 1:-1) * 10;
  pointerCoords.y = y1 - (zoomIn ? 1:-1) * 10;
  coords[0] = pointerCoords;//更新点 1 坐标
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE,
            1, new int[]{0}, coords, 0, 0, 0, 0, 0, 0, 0);//MOVE 事件
  postMotionEvent(imgr, event, 110);//在DOWN 后, 推迟110ms再MOVE
  pointerCoords = new PointerCoords();
  pointerCoords.x = x2 + (zoomIn ? 1:-1) * 10;
  pointerCoords.y = y2 + (zoomIn ? 1:-1) * 10;
  coords[1] = pointerCoords;//同样更新点2坐标
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE,
            2, new int[]{0,1}, coords, 0, 0, 0, 0, 0, 0, 0);
  postMotionEvent(imgr, event, 110);//发送MOVE.
  part 2-2
                //重复MOVE, 有可能移得不够远
  pointerCoords = new PointerCoords();
  pointerCoords.x = x1 - (zoomIn ? 1:-1) * 40;
  pointerCoords.y = y1 - (zoomIn ? 1:-1) * 40;
  coords[0] = pointerCoords;
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE,
            1, new int[]{0}, coords, 0, 0, 0, 0, 0, 0, 0);
  postMotionEvent(imgr, event, 150);
  pointerCoords = new PointerCoords();
  pointerCoords.x = x2 + (zoomIn ? 1:-1) * 40;
  pointerCoords.y = y2 + (zoomIn ? 1:-1) * 40;
  coords[1] = pointerCoords;
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE,
            2, new int[]{0,1}, coords, 0, 0, 0, 0, 0, 0, 0);
  postMotionEvent(imgr, event, 150);
  part 2-3
  pointerCoords = new PointerCoords();
  pointerCoords.x = x1 - (zoomIn ? 1:-1) * 100;
  pointerCoords.y = y1 - (zoomIn ? 1:-1) * 100;
  coords[0] = pointerCoords;
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE,
            1, new int[]{0}, coords, 0, 0, 0, 0, 0, 0, 0);
  postMotionEvent(imgr, event, 180);
  pointerCoords = new PointerCoords();
  pointerCoords.x = x2 + (zoomIn ? 1:-1) * 100;
  pointerCoords.y = y2 + (zoomIn ? 1:-1) * 100;
  coords[1] = pointerCoords;
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE,
            2, new int[]{0,1}, coords, 0, 0, 0, 0, 0, 0, 0);
  postMotionEvent(imgr, event, 180);
  part 3
                //有DOWN 必有 UP
                //坐标不需要更新了, 直接创建EVENT, 并发送即可.
  //第二个手指抬起MotionEvent
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_POINTER_2_UP, 2, new int[]{0,1}, coords, 0, 0, 0, 0, 0, 0, 0);
  postMotionEvent(imgr, event, 300);
  //第一个手指抬起MotionEvent
  event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 1, new int[]{0}, coords, 0, 0, 0, 0, 0, 0, 0);
  postMotionEvent(imgr, event, 300);
  }
  /** move from one location to another **/
  public void postMove(int fx, int fy, int tx, int ty, int delayed){
  long downTime = SystemClock.uptimeMillis();
  InputManager imgr = InputManager.getInstance();
  MotionEvent down = MotionEvent.obtain(downTime,SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, fx, fy, 0);
  postMotionEvent(imgr, down, delayed);
  MotionEvent move = MotionEvent.obtain(downTime,SystemClock.uptimeMillis()+20, MotionEvent.ACTION_MOVE, tx, ty, 0);
  postMotionEvent(imgr, move, delayed);
  MotionEvent up = MotionEvent.obtain(downTime,SystemClock.uptimeMillis()+20, MotionEvent.ACTION_UP, tx, ty, 0);
  postMotionEvent(imgr, up, delayed);
  }
  /** Click in special location **/
        //发送点击事件, DOWN -> UP
  public void postClick(int x, int y, int delayed){
  long downTime = SystemClock.uptimeMillis();
  InputManager imgr = InputManager.getInstance();
  MotionEvent down = MotionEvent.obtain(downTime,SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0);
  postMotionEvent(imgr, down, delayed);
  MotionEvent up = MotionEvent.obtain(downTime,SystemClock.uptimeMillis()+20, MotionEvent.ACTION_UP, x, y, 0);
  postMotionEvent(imgr, up, delayed);
  }


MotionEvent一般是什么?


一般是TouchEvent, KeyEvent, 这两个是十分常见的.


所以, 上面的代码,也可以用于发送按键值, 如BACK, POWER, 'A'


相关文章
|
19小时前
|
Android开发
39. 【Android教程】触摸事件分发
39. 【Android教程】触摸事件分发
7 2
|
20天前
|
存储 Java Linux
Android系统获取event事件回调等几种实现和原理分析
Android系统获取event事件回调等几种实现和原理分析
40 0
|
20天前
|
传感器 Java API
Android Input系统(1) Input事件的产生与传递
Android Input系统(1) Input事件的产生与传递
30 0
|
20天前
|
Android开发
Android事件冲突原理及解决方法
Android事件冲突原理及解决方法
26 0
|
20天前
|
Android开发 容器
[Android]View的事件分发机制(源码解析)
[Android]View的事件分发机制(源码解析)
37 0
|
20天前
|
XML Java Android开发
Android App事件交互Event之模仿京东App实现下拉刷新功能(附源码 可直接使用)
Android App事件交互Event之模仿京东App实现下拉刷新功能(附源码 可直接使用)
43 0
Android App事件交互Event之模仿京东App实现下拉刷新功能(附源码 可直接使用)
|
20天前
|
XML Java Android开发
Android App事件交互中辨别缩放与旋转手指的讲解与实战(附源码 可直接使用)
Android App事件交互中辨别缩放与旋转手指的讲解与实战(附源码 可直接使用)
55 0
|
20天前
|
XML Java Android开发
Android App事件交互中区分点击和长按动作以及识别手势滑动方向的讲解及实战(附源码 可直接使用)
Android App事件交互中区分点击和长按动作以及识别手势滑动方向的讲解及实战(附源码 可直接使用)
99 0
|
20天前
|
XML Java Android开发
Android App开发触摸事件中手势事件Event的分发流程讲解与实战(附源码 简单易懂)
Android App开发触摸事件中手势事件Event的分发流程讲解与实战(附源码 简单易懂)
56 0
|
20天前
|
XML 监控 Java
Android App开发之事件交互Event中检测软键盘和物理按键讲解及实战(附源码 演示简单易懂)
Android App开发之事件交互Event中检测软键盘和物理按键讲解及实战(附源码 演示简单易懂)
212 0