下面列出了android.view.MotionEvent#TOOL_TYPE_STYLUS 实例代码,或者点击链接到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 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 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;
}
}
/**
* 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;
}
/**
* Identifies if the provided {@link MotionEvent} is a stylus with the primary stylus button
* pressed.
*
* @param event The event to check.
* @return Whether a stylus button press occurred.
*/
private static boolean isStylusButtonPressed(MotionEvent event) {
return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS
&& ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY)
== MotionEvent.BUTTON_SECONDARY);
}
/**
* Identifies if the provided {@link MotionEvent} is a stylus with the primary stylus button
* pressed.
*
* @param event The event to check.
* @return Whether a stylus button press occurred.
*/
private static boolean isStylusButtonPressed(MotionEvent event) {
return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS
&& ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY)
== MotionEvent.BUTTON_SECONDARY);
}