下面列出了android.view.MotionEvent#TOOL_TYPE_MOUSE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static int actionToToolType(final W3CItemModel item) {
if (item.parameters != null && item.parameters.pointerType != null) {
switch (item.parameters.pointerType) {
case POINTER_TYPE_MOUSE:
return MotionEvent.TOOL_TYPE_MOUSE;
case POINTER_TYPE_PEN:
return MotionEvent.TOOL_TYPE_STYLUS;
case POINTER_TYPE_TOUCH:
return MotionEvent.TOOL_TYPE_FINGER;
default:
// use default
break;
}
}
return MotionEvent.TOOL_TYPE_FINGER;
}
private static MotionEvent.PointerProperties[] getPointerProperties(int inputDevice) {
MotionEvent.PointerProperties[] pointerProperties = {new MotionEvent.PointerProperties()};
pointerProperties[0].clear();
pointerProperties[0].id = 0;
switch (inputDevice) {
case InputDevice.SOURCE_MOUSE:
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_MOUSE;
break;
case InputDevice.SOURCE_STYLUS:
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_STYLUS;
break;
case InputDevice.SOURCE_TOUCHSCREEN:
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
break;
default:
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_UNKNOWN;
break;
}
return pointerProperties;
}
/**
* Returns a human-readable string describing the type of touch that triggered a MotionEvent.
*/
private static String getTouchType(MotionEvent e){
String touchTypeDescription = " ";
int touchType = e.getToolType(0);
switch (touchType) {
case MotionEvent.TOOL_TYPE_FINGER:
touchTypeDescription += "(finger)";
break;
case MotionEvent.TOOL_TYPE_STYLUS:
touchTypeDescription += "(stylus, ";
//Get some additional information about the stylus touch
float stylusPressure = e.getPressure();
touchTypeDescription += "pressure: " + stylusPressure;
if(Build.VERSION.SDK_INT >= 21) {
touchTypeDescription += ", buttons pressed: " + getButtonsPressed(e);
}
touchTypeDescription += ")";
break;
case MotionEvent.TOOL_TYPE_ERASER:
touchTypeDescription += "(eraser)";
break;
case MotionEvent.TOOL_TYPE_MOUSE:
touchTypeDescription += "(mouse)";
break;
default:
touchTypeDescription += "(unknown tool)";
break;
}
return touchTypeDescription;
}
private void buildMoveEvent(MotionEvent event){
MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[]{new MotionEvent.PointerProperties()};
event.getPointerProperties(0, pointerProperties[0]);
MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[]{new MotionEvent.PointerCoords()};
event.getPointerCoords(0, pointerCoords[0]);
pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_MOUSE;
pointerCoords[0].x -= this.getX();
pointerCoords[0].y -= this.getY();
long touchTime = SystemClock.uptimeMillis();
this.onTouchEvent(MotionEvent.obtain(touchTime, touchTime, MotionEvent.ACTION_MOVE, 1,
pointerProperties, pointerCoords, 0, 0, 0f, 0f,
InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC, 0, InputDevice.SOURCE_TOUCHSCREEN, 0));
}
@Override
public boolean onMouseEvent(final MotionEvent event) {
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE) {
return handleMouseEvent(event);
}
return handleMotionEvent(event);
}
private static void assertPointersCount(int toolType, int pointerIndex) {
if (toolType == MotionEvent.TOOL_TYPE_MOUSE && pointerIndex > 0) {
throw new ActionsParseException(
String.format("No more that one simultaneous pointer is supported for %s %s",
PARAMETERS_KEY_POINTER_TYPE, POINTER_TYPE_MOUSE));
}
if (toolType == MotionEvent.TOOL_TYPE_STYLUS && pointerIndex > 0) {
throw new ActionsParseException(
String.format("No more that one simultaneous pointer is supported for %s %s",
PARAMETERS_KEY_POINTER_TYPE, POINTER_TYPE_PEN));
}
}
private static MotionEvent.PointerProperties[] filterPointerProperties(
final List<MotionInputEventParams> motionEventsParams, final boolean shouldHovering) {
final List<MotionEvent.PointerProperties> result = new ArrayList<>();
for (final MotionInputEventParams eventParams : motionEventsParams) {
if (shouldHovering && HOVERING_ACTIONS.contains(eventParams.actionCode)
&& eventParams.properties.toolType == MotionEvent.TOOL_TYPE_MOUSE) {
result.add(eventParams.properties);
} else if (!shouldHovering && !HOVERING_ACTIONS.contains(eventParams.actionCode)) {
result.add(eventParams.properties);
}
}
return result.toArray(new MotionEvent.PointerProperties[0]);
}
private static MotionEvent.PointerCoords[] filterPointerCoordinates(
final List<MotionInputEventParams> motionEventsParams, final boolean shouldHovering) {
final List<MotionEvent.PointerCoords> result = new ArrayList<>();
for (final MotionInputEventParams eventParams : motionEventsParams) {
if (shouldHovering && HOVERING_ACTIONS.contains(eventParams.actionCode) &&
eventParams.properties.toolType == MotionEvent.TOOL_TYPE_MOUSE) {
result.add(eventParams.coordinates);
} else if (!shouldHovering && !HOVERING_ACTIONS.contains(eventParams.actionCode)) {
result.add(eventParams.coordinates);
}
}
return result.toArray(new MotionEvent.PointerCoords[0]);
}
private static int toolTypeToInputSource(final int toolType) {
switch (toolType) {
case MotionEvent.TOOL_TYPE_MOUSE:
return InputDevice.SOURCE_MOUSE;
case MotionEvent.TOOL_TYPE_STYLUS:
return InputDevice.SOURCE_STYLUS;
default:
return InputDevice.SOURCE_TOUCHSCREEN;
}
}
/**
* @see View#onGenericMotionEvent(MotionEvent)
*/
public boolean onGenericMotionEvent(MotionEvent event) {
if (GamepadList.onGenericMotionEvent(event)) return true;
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_SCROLL:
getEventForwarder().onMouseWheelEvent(event.getEventTime(), event.getX(),
event.getY(), event.getAxisValue(MotionEvent.AXIS_HSCROLL),
event.getAxisValue(MotionEvent.AXIS_VSCROLL),
mRenderCoordinates.getWheelScrollFactor());
return true;
case MotionEvent.ACTION_BUTTON_PRESS:
case MotionEvent.ACTION_BUTTON_RELEASE:
// TODO(mustaq): Should we include MotionEvent.TOOL_TYPE_STYLUS here?
// crbug.com/592082
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE) {
return getEventForwarder().onMouseEvent(event);
}
}
} else if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
if (mJoystickScrollEnabled) {
float velocityX = getFilteredAxisValue(event, MotionEvent.AXIS_X);
float velocityY = getFilteredAxisValue(event, MotionEvent.AXIS_Y);
flingViewport(event.getEventTime(), -velocityX, -velocityY, true);
return true;
}
}
return mContainerViewInternals.super_onGenericMotionEvent(event);
}
/**
* @see View#onTouchEvent(MotionEvent)
*/
public boolean onTouchEvent(MotionEvent event) {
// TODO(mustaq): Should we include MotionEvent.TOOL_TYPE_STYLUS here?
// crbug.com/592082
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE) {
// Mouse button info is incomplete on L and below
int apiVersion = Build.VERSION.SDK_INT;
if (apiVersion >= android.os.Build.VERSION_CODES.M) {
return onMouseEvent(event);
}
}
final boolean isTouchHandleEvent = false;
return sendTouchEvent(event, isTouchHandleEvent);
}
/**
* Returns a human-readable string describing the type of touch that triggered a MotionEvent.
*/
private static String getTouchType(MotionEvent e){
String touchTypeDescription = " ";
int touchType = e.getToolType(0);
switch (touchType) {
case MotionEvent.TOOL_TYPE_FINGER:
touchTypeDescription += "(finger)";
break;
case MotionEvent.TOOL_TYPE_STYLUS:
touchTypeDescription += "(stylus, ";
//Get some additional information about the stylus touch
float stylusPressure = e.getPressure();
touchTypeDescription += "pressure: " + stylusPressure;
if(Build.VERSION.SDK_INT >= 21) {
touchTypeDescription += ", buttons pressed: " + getButtonsPressed(e);
}
touchTypeDescription += ")";
break;
case MotionEvent.TOOL_TYPE_ERASER:
touchTypeDescription += "(eraser)";
break;
case MotionEvent.TOOL_TYPE_MOUSE:
touchTypeDescription += "(mouse)";
break;
default:
touchTypeDescription += "(unknown tool)";
break;
}
return touchTypeDescription;
}
static boolean isMouseEvent(@NonNull MotionEvent e) {
return e.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE;
}