【问题标题】:How to dispatch multi touch gestures using AccessibilityService (disptachGesture)如何使用 AccessibilityService (disptachGesture) 发送多点触控手势
【发布时间】:2022-08-20 22:22:53
【问题描述】:

众所周知,Android OS。支持**多手指手势**。我想开发一个为用户发送复杂手势的应用程序。我能够捕获仅由一根手指构成的运动事件和调度手势。

但是,如果用户使用多个指针(手指)来制作手势,那么我可以捕获它们,但是如何使用 Accessibility Service (dispatchGesture) 函数来调度它们。

任何帮助都将受到欢迎。谢谢

    标签: android gesture accessibilityservice


    【解决方案1】:

    单个指针在运动事件中出现的顺序是未定义的。因此,指针的索引可以从一个事件更改为下一个事件,但只要指针保持活动状态,就可以保证指针的指针 ID 保持不变。使用 getPointerId() 方法获取指针的 ID,以在手势中的所有后续运动事件中跟踪指针。然后对于连续的运动事件,使用 findPointerIndex() 方法获取该运动事件中给定指针 ID 的指针索引。例如:

    private var mActivePointerId: Int = 0
    
    override fun onTouchEvent(event: MotionEvent): Boolean {
        ...
        // Get the pointer ID
        mActivePointerId = event.getPointerId(0)
    
        // ... Many touch events later...
    
        // Use the pointer ID to find the index of the active pointer
        // and fetch its position
        val (x: Float, y: Float) = event.findPointerIndex(mActivePointerId).let { pointerIndex ->
            // Get the pointer's current position
            event.getX(pointerIndex) to event.getY(pointerIndex)
        }
        ...
    }
    

    要支持多个触摸指针,您可以将所有活动指针及其 ID 缓存在各自的位置ACTION_POINTER_DOWNACTION_DOWN活动时间;从缓存中删除指针ACTION_POINTER_UPACTION_UPevents.为了正确处理其他动作事件,可能需要这些缓存的 ID;例如,当处理ACTION_MOVE事件,您可以找到每个缓存的活动指针 ID 的索引,使用相关函数(getX()、getY() 等)检索指针的坐标,然后与缓存的坐标进行比较以发现实际移动的指针。一个可以有多个移动的指针ACTION_MOVE事件。这获取动作索引()功能不适用于ACTION_MOVE事件。

    【讨论】:

    【解决方案2】:

    因此,实际上要使用无障碍服务发送多指手势,我们可以为每个手指使用笔划。 例如,要分派一个两指手势,需要在手势描述中添加两个手势笔划,然后分派它。

    以双扫手势为例

    Point position=new Point(100,10);
    GestureDescription.Builder builder = new GestureDescription.Builder();
    Path p = new Path();
    Path q = new Path();
    //For first finger
    p.moveTo(position.x, position.y);
    p.lineTo(position.x, position.y+300); 
    
    //For second finger
    q.moveTo(position.x, position.y);
    q.lineTo(position.x + 50, position.y+300); 
    
    //Two strokes for two fingers
    builder.addStroke(new GestureDescription.StrokeDescription(p, 100L, 50L));
    builder.addStroke(new GestureDescription.StrokeDescription(q, 100L, 50L));
    GestureDescription gesture = builder.build();
    boolean isDispatched = dispatchGesture(gesture,gestureResultCallback,null);
    

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 2020-12-12
      • 2011-08-22
      相关资源
      最近更新 更多