下面列出了android.view.MotionEvent#getButtonState ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(14)
protected boolean performButtonActionOnTouchDown(MotionEvent event) {
if (VERSION.SDK_INT < 14 || (event.getButtonState() & 2) == 0 || !showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
return false;
}
return true;
}
public boolean onRowTouch(View view, MotionEvent event, LayerRowViewModel row) {
if (row != null) {
currentTouchLayer = row.getLayer();
}
currentTouchView = view;
detector.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
currentTouchIsSecondaryButton = event.getButtonState() == MotionEvent.BUTTON_SECONDARY;
}
return true;
}
/**
* ボタンの押し下げ状態を文字列として返す
* @param event
* @return
*/
public static String getButtonStateString(@NonNull final MotionEvent event) {
int buttonState = event.getButtonState();
if (buttonState == 0) {
return "[]";
}
final StringBuilder result = new StringBuilder("[");
int i = 0;
while (buttonState != 0) {
final boolean isSet = (buttonState & 1) != 0;
buttonState >>>= 1; // unsigned shift!
if (isSet) {
final String name = BUTTON_SYMBOLIC_NAMES[i];
if (result.length() == 1) {
result.append(name);
if (buttonState == 0) {
break;
}
} else {
result.append('|');
result.append(name);
}
}
i += 1;
}
result.append("]");
return result.toString();
}
/**
* Performs button-related actions during a touch down event.
*
* @param event
* The event.
* @return True if the down was consumed.
*
*/
@TargetApi(14)
protected boolean performButtonActionOnTouchDown( MotionEvent event ) {
if ( android.os.Build.VERSION.SDK_INT >= 14 ) {
if ( ( event.getButtonState() & MotionEvent.BUTTON_SECONDARY ) != 0 ) {
if ( showContextMenu( event.getX(), event.getY(), event.getMetaState() ) ) {
return true;
}
}
}
return false;
}
@Override
public void onInputEvent(InputEvent event, int displayId) {
boolean handled = false;
try {
if (!(event instanceof MotionEvent)
|| (event.getSource() & SOURCE_CLASS_POINTER) == 0
|| mMuteInput) {
return;
}
final MotionEvent motionEvent = (MotionEvent) event;
final float newX = motionEvent.getRawX();
final float newY = motionEvent.getRawY();
final boolean isStylusButtonDown =
(motionEvent.getButtonState() & BUTTON_STYLUS_PRIMARY) != 0;
if (mIsStartEvent) {
// First event and the button was down, check for the button being
// lifted in the future, if that happens we'll drop the item.
mStylusButtonDownAtStart = isStylusButtonDown;
mIsStartEvent = false;
}
switch (motionEvent.getAction()) {
case ACTION_DOWN:
if (DEBUG_DRAG) Slog.w(TAG_WM, "Unexpected ACTION_DOWN in drag layer");
return;
case ACTION_MOVE:
if (mStylusButtonDownAtStart && !isStylusButtonDown) {
if (DEBUG_DRAG) {
Slog.d(TAG_WM, "Button no longer pressed; dropping at " + newX + ","
+ newY);
}
mMuteInput = true;
}
break;
case ACTION_UP:
if (DEBUG_DRAG) {
Slog.d(TAG_WM, "Got UP on move channel; dropping at " + newX + "," + newY);
}
mMuteInput = true;
break;
case ACTION_CANCEL:
if (DEBUG_DRAG) Slog.d(TAG_WM, "Drag cancelled!");
mMuteInput = true;
break;
default:
return;
}
mDragDropController.handleMotionEvent(!mMuteInput /* keepHandling */, newX, newY);
handled = true;
} catch (Exception e) {
Slog.e(TAG_WM, "Exception caught by drag handleMotion", e);
} finally {
finishInputEvent(event, handled);
}
}
private static boolean isButtonPressed(MotionEvent e, int button) {
if (button == 0) {
return false;
}
return (e.getButtonState() & button) == button;
}
static boolean isTouchpadScroll(@NonNull MotionEvent e) {
// Touchpad inputs are treated as mouse inputs, and when scrolling, there are no buttons
// returned.
return isMouseEvent(e) && isActionMove(e) && e.getButtonState() == 0;
}
private boolean onTouchOrHoverEvent(MotionEvent event, boolean isTouch) {
final int buttonState = event.getButtonState();
int pressedButtons = buttonState & ~mOldButtonState;
mOldButtonState = buttonState;
if ((pressedButtons & MotionEvent.BUTTON_SECONDARY) != 0) {
// Advance color when the right mouse button or first stylus button
// is pressed.
advanceColor();
}
PaintMode mode;
if ((buttonState & MotionEvent.BUTTON_TERTIARY) != 0) {
// Splat paint when the middle mouse button or second stylus button is pressed.
mode = PaintMode.Splat;
} else if (isTouch || (buttonState & MotionEvent.BUTTON_PRIMARY) != 0) {
// Draw paint when touching or if the primary button is pressed.
mode = PaintMode.Draw;
} else {
// Otherwise, do not paint anything.
return false;
}
final int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE
|| action == MotionEvent.ACTION_HOVER_MOVE) {
final int N = event.getHistorySize();
final int P = event.getPointerCount();
for (int i = 0; i < N; i++) {
for (int j = 0; j < P; j++) {
paint(getPaintModeForTool(event.getToolType(j), mode),
event.getHistoricalX(j, i),
event.getHistoricalY(j, i),
event.getHistoricalPressure(j, i),
event.getHistoricalTouchMajor(j, i),
event.getHistoricalTouchMinor(j, i),
event.getHistoricalOrientation(j, i),
event.getHistoricalAxisValue(MotionEvent.AXIS_DISTANCE, j, i),
event.getHistoricalAxisValue(MotionEvent.AXIS_TILT, j, i));
}
}
for (int j = 0; j < P; j++) {
paint(getPaintModeForTool(event.getToolType(j), mode),
event.getX(j),
event.getY(j),
event.getPressure(j),
event.getTouchMajor(j),
event.getTouchMinor(j),
event.getOrientation(j),
event.getAxisValue(MotionEvent.AXIS_DISTANCE, j),
event.getAxisValue(MotionEvent.AXIS_TILT, j));
}
mCurX = event.getX();
mCurY = event.getY();
}
return true;
}
/**
* 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);
}