コード例 #1
0
    // Retrieves the normalized coordinates (-1 to 1) for any given (x,y)
    // value reported by the Android MotionEvent.
    private boolean getNormalizedCoordinates(MotionEvent motionEvent) {
      InputDevice device = motionEvent.getDevice();
      if (device != null) {
        InputDevice.MotionRange range =
            device.getMotionRange(MotionEvent.AXIS_X, motionEvent.getSource());
        float x = range.getMax() + 1;
        range = motionEvent.getDevice().getMotionRange(MotionEvent.AXIS_Y, motionEvent.getSource());
        float y = range.getMax() + 1;
        this.x = (motionEvent.getX() / x * 2.0f - 1.0f);
        this.y = 1.0f - motionEvent.getY() / y * 2.0f;
        if (motionEvent.getAction() == MotionEvent.ACTION_SCROLL) {
          this.z = (motionEvent.getAxisValue(MotionEvent.AXIS_VSCROLL) > 0 ? -1 : 1);
        } else {
          this.z = 0;
        }

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
          this.isActive = true;
        } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
          this.isActive = false;
        }
        return true;
      }
      return false;
    }
  private void logMotionEvent(MotionEvent event, boolean gen) {
    String s = gen ? "GenericMotionEvent from " : "TouchEvent from ";
    s += printDevice(event.getDeviceId()) + " source " + printSource(event.getSource());

    int pointerCount = event.getPointerCount();
    String action;
    switch (event.getActionMasked()) {
      case MotionEvent.ACTION_CANCEL:
        action = "cancel";
        break;
      case MotionEvent.ACTION_DOWN:
        action = "down";
        break;
      case MotionEvent.ACTION_HOVER_ENTER:
        action = "hover_enter";
        break;
      case MotionEvent.ACTION_HOVER_EXIT:
        action = "hover_exit";
        break;
      case MotionEvent.ACTION_HOVER_MOVE:
        action = "hover_move";
        break;
      case MotionEvent.ACTION_MOVE:
        action = "move";
        break;
      case MotionEvent.ACTION_OUTSIDE:
        action = "outside";
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        action = "pointer_down";
        break;
      case MotionEvent.ACTION_POINTER_UP:
        action = "pointer_up";
        break;
      case MotionEvent.ACTION_UP:
        action = "up";
        break;
      case MotionEvent.ACTION_SCROLL:
        action = "scroll";
        break;
      default:
        action = "" + event.getAction();
        break;
    }
    s +=
        ", action "
            + action
            + " pointer count "
            + pointerCount
            + " buttons "
            + event.getButtonState()
            + " RawX "
            + event.getRawX()
            + " RawY "
            + event.getRawY()
            + " MetaState "
            + event.getMetaState()
            + " Flags "
            + event.getFlags()
            + "\n";
    for (int p = 0; p < pointerCount; p++) {
      int ptrId = event.getPointerId(p);
      s +=
          "Pointer id "
              + ptrId
              + " X "
              + event.getX(p)
              + " Y "
              + event.getY(p)
              + " pressure "
              + event.getPressure(p)
              + " size "
              + event.getSize(p)
              + " orientation "
              + event.getOrientation(p)
              + // (float)Math.toDegrees(event.getOrientation(p)) +
              " TouchMajor "
              + event.getTouchMajor(p)
              + " TouchMinor "
              + event.getTouchMinor(p)
              + " ToolMajor "
              + event.getToolMajor(p)
              + " ToolMinor "
              + event.getToolMinor(p)
              + "\n";
      for (int i = 0; i < 255; i++) {
        if (event.getAxisValue(i, p) != 0.0) {
          s += "Axis " + i + " " + event.axisToString(i) + ": " + event.getAxisValue(i, p);
          InputDevice device = InputDevice.getDevice(event.getDeviceId());
          if (device != null) {
            InputDevice.MotionRange range = device.getMotionRange(i);
            if (range != null) s += " range " + range.getMin() + " to " + range.getMax();
          }
          s += "\n";
        }
      }
    }

    pushText(s);
  }