获得ACTION_SCROLL MotionEvent的正确方法

时间:2017-12-28 11:04:47

标签: android motionevent

我有一个以编程方式注入鼠标滚动事件的任务。滚动可以是水平和垂直。为了实现这一点,我决定将MotionEvent与ACTION_SCROLL一起使用。但我不清楚如何初始化此事件类型。应该如何定义AXIS_VSCROLL和AXIS_HSCROLL中的滚动偏移量?

我尝试从USB鼠标记录实际的Scroll MotionEvent:

MotionEvent DownTime 91208223
MotionEvent EventTime 91221679
MotionEvent Action 8
MotionEvent PointerCount 1

MotionEvent.PointerCoords x 98.14349
MotionEvent.PointerCoords y 23.289246
MotionEvent.PointerCoords size 0.0
MotionEvent.PointerCoords pressure 0.0
MotionEvent.PointerCoords orientation 0.0
MotionEvent.PointerCoords toolMajor 0.0
MotionEvent.PointerCoords toolMinor 0.0
MotionEvent.PointerCoords toolMinor 0.0
MotionEvent.PointerCoords touchMajor 0.0
MotionEvent.PointerCoords AXIS_HSCROLL 0.0
MotionEvent.PointerCoords AXIS_VSCROLL -1.0

MotionEvent.PointerProperties id 0
MotionEvent.PointerProperties toolType 3

MotionEvent MetaState 0
MotionEvent ButtonState 0
MotionEvent XPrecision 1.0
MotionEvent YPrecision 1.0
MotionEvent DeviceId 8
MotionEvent EdgeFlags 0
MotionEvent Source 8194
MotionEvent Flags 2

并根据这些日志尝试获取自己的事件:

    MotionEvent.PointerCoords coord = new MotionEvent.PointerCoords();
    coord.x = 500f;
    coord.y = 500f;
    coord.setAxisValue(MotionEvent.AXIS_VSCROLL, 1f);
    MotionEvent.PointerCoords[] coords = { coord };

    MotionEvent.PointerProperties properties = new MotionEvent.PointerProperties();
    properties.toolType = 3;
    MotionEvent.PointerProperties[] prop = { properties };

    MotionEvent mouseEvent = MotionEvent.obtain(downTime, downTime + 100,
            MotionEvent.ACTION_SCROLL, 1, prop, coords, 0, 0, 1f, 1f,
            8, 0, 8194, 0);

获取()方法参数:

    MotionEvent obtain (
        long downTime,
        long eventTime,
        int action,
        int pointerCount,
        PointerProperties[] pointerProperties,
        PointerCoords[] pointerCoords,
        int metaState,
        int buttonState,
        float xPrecision,
        float yPrecision,
        int deviceId,
        int edgeFlags,
        int source,
        int flags
    )

1 个答案:

答案 0 :(得分:1)

感谢来自@pskink的方向性问题,构建了以下事件:

    MotionEvent.PointerCoords coord = new MotionEvent.PointerCoords();
    coord.x = x;
    coord.y = y;
    coord.setAxisValue(MotionEvent.AXIS_VSCROLL, 1f);
    MotionEvent.PointerCoords[] coords = { coord };

    MotionEvent.PointerProperties properties = new MotionEvent.PointerProperties();
    properties.id = 0;
    MotionEvent.PointerProperties[] prop = { properties };

    MotionEvent scrollEvent = MotionEvent.obtain(
    SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_SCROLL,
    1, prop, coords, 0, 0, 1f, 1f, 0, 0, SOURCE_CLASS_POINTER, 0);

    dispatchEvent(scrollEvent);