下面列出了android.view.MotionEvent#getToolType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
public void handleTouch(MotionEvent event) {
Log.d("touchEvent", event.getToolType(0) + ":" + event.getActionMasked());
if(event.getToolType(0) == MotionEvent.TOOL_TYPE_FINGER) {
if(event.getAction() == MotionEvent.ACTION_MOVE) {
buildMoveEvent(event);
} else if(event.getAction() == MotionEvent.ACTION_DOWN && changeTouchEvent){
buildMoveEvent(event);
} else if(event.getAction() == MotionEvent.ACTION_UP && changeTouchEvent){
buildMoveEvent(event);
}
}
}
@Override
public boolean onMouseEvent(final MotionEvent event) {
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE) {
return handleMouseEvent(event);
}
return handleMotionEvent(event);
}
/**
* @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;
}
static boolean isFingerEvent(@NonNull MotionEvent e) {
return e.getToolType(0) == MotionEvent.TOOL_TYPE_FINGER;
}
/**
* 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);
}