获取ACTION_SCROLL MotionEvent的正确方法

Proper way to obtain ACTION_SCROLL 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
    )

感谢@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);