android.view.MotionEvent#getDevice ( )源码实例Demo

下面列出了android.view.MotionEvent#getDevice ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: VCL-Android   文件: AudioPlayerActivity.java
public boolean dispatchGenericMotionEvent(MotionEvent event){
    //Check for a joystick event
    if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) !=
            InputDevice.SOURCE_JOYSTICK ||
            event.getAction() != MotionEvent.ACTION_MOVE)
        return false;

    InputDevice inputDevice = event.getDevice();

    float dpadx = event.getAxisValue(MotionEvent.AXIS_HAT_X);
    float dpady = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
    if (inputDevice == null || Math.abs(dpadx) == 1.0f || Math.abs(dpady) == 1.0f)
        return false;

    float x = AndroidDevices.getCenteredAxis(event, inputDevice,
            MotionEvent.AXIS_X);

    if (System.currentTimeMillis() - mLastMove > JOYSTICK_INPUT_DELAY){
        if (Math.abs(x) > 0.3){
            seek(x > 0.0f ? 10000 : -10000);
            mLastMove = System.currentTimeMillis();
            return true;
        }
    }
    return true;
}
 
源代码2 项目: Alite   文件: AndroidView.java
private void processJoystickInput(Screen screen, MotionEvent event, int historyPos) {
	if (screen == null) {
		return;
	}
    InputDevice inputDevice = event.getDevice();
    float z = getCenteredAxis(event, inputDevice, MotionEvent.AXIS_Z, historyPos);
    float rz = getCenteredAxis(event, inputDevice, MotionEvent.AXIS_RZ, historyPos);
    screen.processNavigationJoystick(z, rz);
    screen.processJoystick(
    		getCenteredAxis(event, inputDevice, MotionEvent.AXIS_X, historyPos),
    		getCenteredAxis(event, inputDevice, MotionEvent.AXIS_Y, historyPos),
    		z,
    		rz,
    		getCenteredAxis(event, inputDevice, MotionEvent.AXIS_HAT_X, historyPos),
    		getCenteredAxis(event, inputDevice, MotionEvent.AXIS_HAT_Y, historyPos));
}
 
@Override
public boolean onGenericMotionEvent(final MotionEvent ev) {
    // Log.d(TAG, "onGenericMotionEvent: " + ev);
    InputDevice device = ev.getDevice();
    // Only care about game controllers.
    if (device != null && device.getId() == mCurrentDeviceId) {
        if (isGamepad(device)) {
            for (AxesMapping axesMapping : AxesMapping.values()) {
                mAxes[axesMapping.ordinal()] = getCenteredAxis(ev, device,
                        axesMapping.getMotionEvent());
            }
            mControllerView.invalidate();
            return true;
        }
    }
    return super.onGenericMotionEvent(ev);
}
 
public void dealWithMotionEvent(MotionEvent event){
    //Log.i(LOG_TAG, "Input device: " + event.getDevice().getName());
    /*if axis has a range of 1 (0 -> 1) instead of 2 (-1 -> 0) do not invert axis value,
    this is necessary for analog R1 (Brake) or analog R2 (Gas) shoulder buttons on PS3 controller*/
    if (event != null) {
        InputDevice device = event.getDevice();
        if (device != null) {
            mRightAnalogYAxisInvertFactor = (device.getMotionRange(mRightAnalogYAxis).getRange() == 1) ? 1 : -1;
            mLeftAnalogYAxisInvertFactor = (device.getMotionRange(mLeftAnalogYAxis).getRange() == 1) ? 1 : -1;
        } else {
            Log.w(LOG_TAG, "event.getDevice() == null! => event.getClass(): " + event.getClass());
        }

        // default axis are set to work with PS3 controller
        mControls.setRightAnalogX((float) event.getAxisValue(mRightAnalogXAxis));
        mControls.setRightAnalogY((float) (event.getAxisValue(mRightAnalogYAxis)) * mRightAnalogYAxisInvertFactor);
        mControls.setLeftAnalogX((float) event.getAxisValue(mLeftAnalogXAxis));
        mControls.setLeftAnalogY((float) (event.getAxisValue(mLeftAnalogYAxis)) * mLeftAnalogYAxisInvertFactor);

        mSplit_axis_yaw_right = (float) event.getAxisValue(mSplitAxisYawRightAxis);
        mSplit_axis_yaw_left = (float) event.getAxisValue(mSplitAxisYawLeftAxis);
    } else {
        Log.w(LOG_TAG, "event == null!");
    }
}
 
源代码5 项目: bluetooth   文件: GameView.java
/**
 * The ship directly handles joystick input.
 *
 * @param event
 * @param historyPos
 */
private void processJoystickInput(MotionEvent event, int historyPos) {
    // Get joystick position.
    // Many game pads with two joysticks report the position of the
    // second
    // joystick
    // using the Z and RZ axes so we also handle those.
    // In a real game, we would allow the user to configure the axes
    // manually.
    if (null == mInputDevice) {
        mInputDevice = event.getDevice();
    }
    float x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_X, historyPos);
    if (x == 0) {
        x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_HAT_X, historyPos);
    }
    if (x == 0) {
        x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Z, historyPos);
    }

    float y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Y, historyPos);
    if (y == 0) {
        y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_HAT_Y, historyPos);
    }
    if (y == 0) {
        y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RZ, historyPos);
    }

    // Set the ship heading.
    setHeading(x, y);
    GameView.this.step(historyPos < 0 ? event.getEventTime() : event
            .getHistoricalEventTime(historyPos));
}
 
源代码6 项目: codeexamples-android   文件: GameView.java
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    ensureInitialized();

    // 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) {
        // Cache the most recently obtained device information.
        // The device information may change over time but it can be
        // somewhat expensive to query.
        if (mLastInputDevice == null || mLastInputDevice.getId() != event.getDeviceId()) {
            mLastInputDevice = event.getDevice();
            // It's possible for the device id to be invalid.
            // In that case, getDevice() will return null.
            if (mLastInputDevice == null) {
                return false;
            }
        }

        // Ignore joystick while the DPad is pressed to avoid conflicting motions.
        if (mDPadState != 0) {
            return true;
        }

        // Process all historical movement samples in the batch.
        final int historySize = event.getHistorySize();
        for (int i = 0; i < historySize; i++) {
            processJoystickInput(event, i);
        }

        // Process the current movement sample in the batch.
        processJoystickInput(event, -1);
        return true;
    }
    return super.onGenericMotionEvent(event);
}
 
源代码7 项目: citra_android   文件: EmulationActivity.java
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event)
{
  if (mMenuVisible)
  {
    return false;
  }

  if (((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0))
  {
    return super.dispatchGenericMotionEvent(event);
  }

  // Don't attempt to do anything if we are disconnecting a device.
  if (event.getActionMasked() == MotionEvent.ACTION_CANCEL)
    return true;

  InputDevice input = event.getDevice();
  List<InputDevice.MotionRange> motions = input.getMotionRanges();

  float[] axisValues = {0.0f, 0.0f};
  for (InputDevice.MotionRange range : motions)
  {
    boolean consumed = false;
    int axis = range.getAxis();
    float origValue = event.getAxisValue(axis);
    float value = mControllerMappingHelper.scaleAxis(input, axis, origValue);

    if (axis == AXIS_X || axis == AXIS_Z)
    {
      axisValues[0] = value;
    }
    else if (axis == AXIS_Y || axis == AXIS_RZ)
    {
      axisValues[1] = value;
    }

    // If the input is still in the "flat" area, that means it's really zero.
    // This is used to compensate for imprecision in joysticks.
    if (Math.abs(axisValues[0]) > range.getFlat() || Math.abs(axisValues[1]) > range.getFlat())
    {
      consumed = NativeLibrary.onGamePadMoveEvent(input.getDescriptor(), axis, axisValues[0], axisValues[1]);
    }
    else
    {
      consumed = NativeLibrary.onGamePadMoveEvent(input.getDescriptor(), axis, 0.0f, 0.0f);
    }

    return NativeLibrary.onGamePadAxisEvent(input.getDescriptor(),axis, value) || consumed;
  }

  return false;
}
 
源代码8 项目: citra_android   文件: MotionAlertDialog.java
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;
}
 
源代码9 项目: VCL-Android   文件: VideoPlayerActivity.java
@TargetApi(12) //only active for Android 3.1+
public boolean dispatchGenericMotionEvent(MotionEvent event){
    if (mIsLoading)
        return  false;
    //Check for a joystick event
    if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) !=
            InputDevice.SOURCE_JOYSTICK ||
            event.getAction() != MotionEvent.ACTION_MOVE)
        return false;

    InputDevice mInputDevice = event.getDevice();

    float dpadx = event.getAxisValue(MotionEvent.AXIS_HAT_X);
    float dpady = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
    if (mInputDevice == null || Math.abs(dpadx) == 1.0f || Math.abs(dpady) == 1.0f)
        return false;

    float x = AndroidDevices.getCenteredAxis(event, mInputDevice,
            MotionEvent.AXIS_X);
    float y = AndroidDevices.getCenteredAxis(event, mInputDevice,
            MotionEvent.AXIS_Y);
    float rz = AndroidDevices.getCenteredAxis(event, mInputDevice,
            MotionEvent.AXIS_RZ);

    if (System.currentTimeMillis() - mLastMove > JOYSTICK_INPUT_DELAY){
        if (Math.abs(x) > 0.3){
            if (BuildConfig.tv) {
                navigateDvdMenu(x > 0.0f ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT);
            } else
                seekDelta(x > 0.0f ? 10000 : -10000);
        } else if (Math.abs(y) > 0.3){
            if (BuildConfig.tv)
                navigateDvdMenu(x > 0.0f ? KeyEvent.KEYCODE_DPAD_UP : KeyEvent.KEYCODE_DPAD_DOWN);
            else {
                if (mIsFirstBrightnessGesture)
                    initBrightnessTouch();
                changeBrightness(-y / 10f);
            }
        } else if (Math.abs(rz) > 0.3){
            mVol = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            int delta = -(int) ((rz / 7) * mAudioMax);
            int vol = (int) Math.min(Math.max(mVol + delta, 0), mAudioMax);
            setAudioVolume(vol);
        }
        mLastMove = System.currentTimeMillis();
    }
    return true;
}