下面列出了android.view.MotionEvent#getSource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onHover(View view, MotionEvent event) {
if (view != getView()) {
return false;
}
boolean consumed = false;
int source = event.getSource();
// logger.log(Level.INFO, "onTouch source: {0}", source);
boolean isTouch = ((source & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN);
// logger.log(Level.INFO, "onTouch source: {0}, isTouch: {1}",
// new Object[]{source, isTouch});
if (isTouch && touchInput != null) {
// send the event to the touch processor
consumed = ((AndroidTouchInput14)touchInput).onHover(event);
}
return consumed;
}
@Override
public boolean onGenericMotion(View v, MotionEvent event) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0
&& event.getAction() == MotionEvent.ACTION_MOVE) {
List<MotionRange> motionRanges = event.getDevice().getMotionRanges();
for(MotionRange mr : motionRanges){
int axis = mr.getAxis();
if(event.getAxisValue(axis) > 0.5 || event.getAxisValue(axis) < -0.5){
Log.i(TAG, "Axis found: " + MotionEvent.axisToString(axis));
this.mAxisName = MotionEvent.axisToString(axis);
mValueTextView.setText(mAxisName);
}
}
}else{
Log.i(TAG, "Not a joystick event.");
}
return true;
}
public boolean onGenericMotionEvent(MotionEvent event)
{
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0)
{
switch (event.getAction())
{
case MotionEvent.ACTION_SCROLL:
{
final float hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
final float vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
if (hscroll != 0.0f || vscroll != 0.0f)
{
mListener.onMouseScroll(hscroll, vscroll);
}
}
}
}
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (Build.VERSION.SDK_INT >= 12) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL: {
if (mTouchMode == TOUCH_MODE_REST) {
final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
if (vscroll != 0) {
final int delta = (int) (vscroll * getVerticalScrollFactor());
if (!trackMotionScroll(delta, delta)) {
return true;
}
}
}
}
}
}
}
return super.onGenericMotionEvent(event);
}
@Override
public boolean onGenericMotion(View view, MotionEvent event) {
if (view != getView()) {
return false;
}
boolean consumed = false;
int source = event.getSource();
// logger.log(Level.INFO, "onGenericMotion source: {0}", source);
boolean isJoystick =
((source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK);
if (isJoystick && joyInput != null) {
// logger.log(Level.INFO, "onGenericMotion source: {0}, isJoystick: {1}",
// new Object[]{source, isJoystick});
// send the event to the touch processor
consumed = consumed || ((AndroidJoyInput14)joyInput).onGenericMotion(event);
}
return consumed;
}
@TargetApi(12)
public boolean onGenericMotionEvent(MotionEvent event) {
if ((event.getSource() & 2) != 0) {
switch (event.getAction()) {
case 8:
if (this.mTouchMode == -1) {
float hscroll = event.getAxisValue(10);
if (hscroll != 0.0f) {
int delta = (int) (getHorizontalScrollFactor() * hscroll);
if (!trackMotionScroll(delta, delta)) {
return true;
}
}
}
break;
}
}
return super.onGenericMotionEvent(event);
}
private MotionEvent transformEvent(MotionEvent e, int headerPosition) {
if (headerPosition == MATCHED_STICKIED_HEADER) {
return e;
}
long downTime = e.getDownTime();
long eventTime = e.getEventTime();
int action = e.getAction();
int pointerCount = e.getPointerCount();
int[] pointerIds = getPointerIds(e);
MotionEvent.PointerCoords[] pointerCoords = getPointerCoords(e);
int metaState = e.getMetaState();
float xPrecision = e.getXPrecision();
float yPrecision = e.getYPrecision();
int deviceId = e.getDeviceId();
int edgeFlags = e.getEdgeFlags();
int source = e.getSource();
int flags = e.getFlags();
View headerHolder = getChildAt(headerPosition);
for (int i = 0; i < pointerCount;i++) {
pointerCoords[i].y -= headerHolder.getTop();
}
MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
yPrecision, deviceId, edgeFlags, source, flags);
return n;
}
private static MotionEvent transformEventOld(MotionEvent e, Matrix m) {
long downTime = e.getDownTime();
long eventTime = e.getEventTime();
int action = e.getAction();
int pointerCount = e.getPointerCount();
int[] pointerIds = getPointerIds(e);
PointerCoords[] pointerCoords = getPointerCoords(e);
int metaState = e.getMetaState();
float xPrecision = e.getXPrecision();
float yPrecision = e.getYPrecision();
int deviceId = e.getDeviceId();
int edgeFlags = e.getEdgeFlags();
int source = e.getSource();
int flags = e.getFlags();
// Copy the x and y coordinates into an array, map them, and copy back.
float[] xy = new float[pointerCoords.length * 2];
for (int i = 0; i < pointerCount;i++) {
xy[2 * i] = pointerCoords[i].x;
xy[2 * i + 1] = pointerCoords[i].y;
}
m.mapPoints(xy);
for (int i = 0; i < pointerCount;i++) {
pointerCoords[i].x = xy[2 * i];
pointerCoords[i].y = xy[2 * i + 1];
pointerCoords[i].orientation = transformAngle(
m, pointerCoords[i].orientation);
}
MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
yPrecision, deviceId, edgeFlags, source, flags);
return n;
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL: {
// Handle mouse (or ext. device) by shifting the page depending on the scroll
final float vscroll;
final float hscroll;
if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
vscroll = 0;
hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
} else {
vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
}
if (hscroll != 0 || vscroll != 0) {
boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
: (hscroll > 0 || vscroll > 0);
if (isForwardScroll) {
scrollRight();
} else {
scrollLeft();
}
return true;
}
}
}
}
return super.onGenericMotionEvent(event);
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_SCROLL &&
(event.getSource() & InputDevice.SOURCE_MOUSE) != 0)
return false;
return super.onGenericMotionEvent(event);
}
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
// Check that the event came from a joystick since a generic motion event
// could be almost anything.
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0
&& event.getAction() == MotionEvent.ACTION_MOVE) {
// Update device state for visualization and logging.
InputDeviceState state = getInputDeviceState(event);
if (state != null && state.onJoystickMotion(event)) {
mSummaryAdapter.show(state);
}
}
return super.dispatchGenericMotionEvent(event);
}
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
// Check that the event came from a joystick since a generic motion event could be almost anything.
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0 && event.getAction() == MotionEvent.ACTION_MOVE && mController instanceof GamepadController) {
mGamepadController.dealWithMotionEvent(event);
updateFlightData();
return true;
} else {
return super.dispatchGenericMotionEvent(event);
}
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (game.getCurrentScreen() == null) {
return true;
}
if (isController(event.getSource())) {
if (Dpad.isDpadDevice(event)) {
if (game.getCurrentScreen() != null) {
game.getCurrentScreen().processDPad(dpad.getDirectionPressed(event));
}
}
// Check that the event came from a game controller
if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) ==
InputDevice.SOURCE_JOYSTICK &&
event.getAction() == MotionEvent.ACTION_MOVE) {
// Process all historical movement samples in the batch
final int historySize = event.getHistorySize();
// Process the movements starting from the
// earliest historical position in the batch
for (int i = 0; i < historySize; i++) {
// Process the event at historical position i
processJoystickInput(game.getCurrentScreen(), event, i);
}
// Process the current movement sample in the batch (position -1)
processJoystickInput(game.getCurrentScreen(), event, -1);
}
}
return super.onGenericMotionEvent(event);
}
static public int getSourceOfEvent( MotionEvent event )
{
return event.getSource();
}
private static boolean isFromTouchpadInGlassDevice(MotionEvent motionEvent) {
return (Build.DEVICE.contains("glass")
|| Build.DEVICE.contains("Glass")
|| Build.DEVICE.contains("wingman"))
&& ((motionEvent.getSource() & InputDevice.SOURCE_TOUCHPAD) != 0);
}
private boolean onMotionEvent(MotionEvent event)
{
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0)
return false;
if (event.getAction() != MotionEvent.ACTION_MOVE)
return false;
InputDevice input = event.getDevice();
List<InputDevice.MotionRange> motionRanges = input.getMotionRanges();
int numMovedAxis = 0;
float axisMoveValue = 0.0f;
InputDevice.MotionRange lastMovedRange = null;
char lastMovedDir = '?';
if (mWaitingForEvent)
{
// Get only the axis that seem to have moved (more than .5)
for (InputDevice.MotionRange range : motionRanges)
{
int axis = range.getAxis();
float origValue = event.getAxisValue(axis);
float value = mControllerMappingHelper.scaleAxis(input, axis, origValue);
if (Math.abs(value) > 0.5f)
{
// It is common to have multiple axis with the same physical input. For example,
// shoulder butters are provided as both AXIS_LTRIGGER and AXIS_BRAKE.
// To handle this, we ignore an axis motion that's the exact same as a motion
// we already saw. This way, we ignore axis with two names, but catch the case
// where a joystick is moved in two directions.
// ref: bottom of https://developer.android.com/training/game-controllers/controller-input.html
if (value != axisMoveValue)
{
axisMoveValue = value;
numMovedAxis++;
lastMovedRange = range;
lastMovedDir = value < 0.0f ? '-' : '+';
}
}
}
// If only one axis moved, that's the winner.
if (numMovedAxis == 1)
{
mWaitingForEvent = false;
saveMotionInput(input, lastMovedRange, lastMovedDir);
}
}
return true;
}
/**
* Dispatch a pointer event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the pointer action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendPointerSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* Dispatch a trackball event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the trackball action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendTrackballEventSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_TRACKBALL) == 0) {
event.setSource(InputDevice.SOURCE_TRACKBALL);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* Dispatch a trackball event. Finished at some point after the recipient has
* returned from its event processing, though it may <em>not</em> have
* completely finished reacting from the event -- for example, if it needs
* to update its display as a result, it may still be in the process of
* doing that.
*
* @param event A motion event describing the trackball action. (As noted in
* {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
* {@link SystemClock#uptimeMillis()} as the timebase.
*/
public void sendTrackballEventSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_TRACKBALL) == 0) {
event.setSource(InputDevice.SOURCE_TRACKBALL);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
/**
* 指定したMotionEventの入力ソースが指定したクラスかどうかをチェック
* @param event
* @param sourceClass
* @return
*/
public static boolean isFromSource(@NonNull final MotionEvent event,
final int sourceClass) {
return (event.getSource() & sourceClass) == sourceClass;
}