Android 获取ACTION\u SCROLL MotionEvent的正确方法
我有一个任务以编程方式注入鼠标滚动事件。滚动可以是水平的,也可以是垂直的。为了实现这一点,我决定将MotionEvent与ACTION_SCROLL结合使用。但我不清楚如何初始化此事件类型。如何定义轴垂直滚动和轴垂直滚动中的滚动偏移 我试图从USB鼠标记录实际的滚动运动事件:Android 获取ACTION\u SCROLL MotionEvent的正确方法,android,motionevent,Android,Motionevent,我有一个任务以编程方式注入鼠标滚动事件。滚动可以是水平的,也可以是垂直的。为了实现这一点,我决定将MotionEvent与ACTION_SCROLL结合使用。但我不清楚如何初始化此事件类型。如何定义轴垂直滚动和轴垂直滚动中的滚动偏移 我试图从USB鼠标记录实际的滚动运动事件: MotionEvent DownTime 91208223 MotionEvent EventTime 91221679 MotionEvent Action 8 MotionEvent PointerCount 1 M
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);
胡乱猜测:使用
MotionEvent.PointerCoords
并将其传递给MotionEvent#acquire()
method?@pskink,听起来是一个可行的解决方案。但是我没有得到积极的结果。可能是指针指针指针
没有正确初始化?是否尝试调用MotionEvent#getPointerCoords
查看典型值?还有3
是鼠标吗?为什么停机时间+100?实际上问题出在pointerProperties.id中,默认设置为-1ahh,很高兴知道,谢谢