Пример #1
0
  // Touch events
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    final int touchDevId = event.getDeviceId();
    final int pointerCount = event.getPointerCount();
    // touchId, pointerId, action, x, y, pressure
    int actionPointerIndex =
        (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
            >> MotionEvent.ACTION_POINTER_ID_SHIFT; /* API 8: event.getActionIndex(); */
    int pointerFingerId = event.getPointerId(actionPointerIndex);
    int action =
        (event.getAction() & MotionEvent.ACTION_MASK); /* API 8: event.getActionMasked(); */

    float x = event.getX(actionPointerIndex) / mWidth;
    float y = event.getY(actionPointerIndex) / mHeight;
    float p = event.getPressure(actionPointerIndex);

    if (action == MotionEvent.ACTION_MOVE && pointerCount > 1) {
      // TODO send motion to every pointer if its position has
      // changed since prev event.
      for (int i = 0; i < pointerCount; i++) {
        pointerFingerId = event.getPointerId(i);
        x = event.getX(i) / mWidth;
        y = event.getY(i) / mHeight;
        p = event.getPressure(i);
        SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
      }
    } else {
      SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
    }
    return true;
  }
Пример #2
0
  // Touch events
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    /* Ref: http://developer.android.com/training/gestures/multi.html */
    final int touchDevId = event.getDeviceId();
    final int pointerCount = event.getPointerCount();
    int action = event.getActionMasked();
    int pointerFingerId;
    int i = -1;
    float x, y, p;

    switch (action) {
      case MotionEvent.ACTION_MOVE:
        for (i = 0; i < pointerCount; i++) {
          pointerFingerId = event.getPointerId(i);
          x = event.getX(i) / mWidth;
          y = event.getY(i) / mHeight;
          p = event.getPressure(i);
          SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
        }
        break;

      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_DOWN:
        // Primary pointer up/down, the index is always zero
        i = 0;
      case MotionEvent.ACTION_POINTER_UP:
      case MotionEvent.ACTION_POINTER_DOWN:
        // Non primary pointer up/down
        if (i == -1) {
          i = event.getActionIndex();
        }

        pointerFingerId = event.getPointerId(i);
        x = event.getX(i) / mWidth;
        y = event.getY(i) / mHeight;
        p = event.getPressure(i);
        SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
        break;

      default:
        break;
    }

    return true;
  }
Пример #3
0
  // Touch events
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    /* Ref: http://developer.android.com/training/gestures/multi.html */
    final int touchDevId = event.getDeviceId();
    final int pointerCount = event.getPointerCount();
    int action = event.getActionMasked();
    int pointerFingerId;
    int mouseButton;
    int i = -1;
    float x, y, p;

    // !!! FIXME: dump this SDK check after 2.0.4 ships and require API14.
    if (event.getSource() == InputDevice.SOURCE_MOUSE && SDLActivity.mSeparateMouseAndTouch) {
      if (Build.VERSION.SDK_INT < 14) {
        mouseButton = 1; // For Android==12 all mouse buttons are the left button
      } else {
        try {
          mouseButton = (Integer) event.getClass().getMethod("getButtonState").invoke(event);
        } catch (Exception e) {
          mouseButton = 1; // oh well.
        }
      }
      SDLActivity.onNativeMouse(mouseButton, action, event.getX(0), event.getY(0));
    } else {
      switch (action) {
        case MotionEvent.ACTION_MOVE:
          for (i = 0; i < pointerCount; i++) {
            pointerFingerId = event.getPointerId(i);
            x = event.getX(i) / mWidth;
            y = event.getY(i) / mHeight;
            p = event.getPressure(i);
            SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
          }
          break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_DOWN:
          // Primary pointer up/down, the index is always zero
          i = 0;
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_POINTER_DOWN:
          // Non primary pointer up/down
          if (i == -1) {
            i = event.getActionIndex();
          }

          pointerFingerId = event.getPointerId(i);
          x = event.getX(i) / mWidth;
          y = event.getY(i) / mHeight;
          p = event.getPressure(i);
          SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
          break;

        case MotionEvent.ACTION_CANCEL:
          for (i = 0; i < pointerCount; i++) {
            pointerFingerId = event.getPointerId(i);
            x = event.getX(i) / mWidth;
            y = event.getY(i) / mHeight;
            p = event.getPressure(i);
            SDLActivity.onNativeTouch(touchDevId, pointerFingerId, MotionEvent.ACTION_UP, x, y, p);
          }
          break;

        default:
          break;
      }
    }

    return true;
  }
Пример #4
0
  public boolean onTouchEvent(MotionEvent ev) {
    if (mapThreadRunning) return false; // no updates when download is running
    if (gestureDetector.onTouchEvent(ev)) return true;

    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        {
          mLastTouchX = ev.getX();
          mLastTouchY = ev.getY();
          mActivePointerId = ev.getPointerId(0);
          break;
        }
      case MotionEvent.ACTION_POINTER_DOWN:
        {
          mLastTouchX2 = ev.getX();
          mLastTouchY2 = ev.getY();
          if (ev.getPointerCount() > 1) mActivePointerId2 = ev.getPointerId(1);
          break;
        }
      case MotionEvent.ACTION_MOVE:
        {
          int pointerIndex;
          float x = 0.0f, y = 0.0f;

          try {
            if ((mActivePointerId != INVALID_POINTER_ID)
                || (mActivePointerId2 != INVALID_POINTER_ID)) {
              pointerIndex = ev.findPointerIndex(mActivePointerId);
              x = ev.getX(pointerIndex);
              y = ev.getY(pointerIndex);
            }
            if ((mActivePointerId != INVALID_POINTER_ID)
                && (mActivePointerId2 != INVALID_POINTER_ID)) {
              double d1, d2;

              pointerIndex = ev.findPointerIndex(mActivePointerId2);
              if (pointerIndex < 0) return false;
              float x2 = ev.getX(pointerIndex);
              float y2 = ev.getY(pointerIndex);

              d1 = java.lang.Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
              d2 =
                  java.lang.Math.sqrt(
                      (mLastTouchX - mLastTouchX2) * (mLastTouchX - mLastTouchX2)
                          + (mLastTouchY - mLastTouchY2) * (mLastTouchY - mLastTouchY2));

              /*                  if ((Math.abs(d1)>300) || (Math.abs(d2)>300))
              {
                 int i=0;
                 i=i;
              }*/

              if ((d1 > 0) && (d2 > 0)) {
                float w, h, s;

                s = (float) (d1 / d2);
                mScaleFactor *= s;
                matrix.postScale(s, s);
                w = scrWidth / 2.0f;
                h = scrHeight / 2.0f;

                matrix.postTranslate(w, h);
                imgOffsetX += w;
                imgOffsetY += h;
              }

              mLastTouchX2 = x2;
              mLastTouchY2 = y2;
            } else if (mScaleFactor == 1.0) {
              mScaleFactor = 1.0f;
              imgOffsetX += (x - mLastTouchX);
              imgOffsetY += (y - mLastTouchY);
              matrix.setTranslate(imgOffsetX, imgOffsetY);
            }

            if ((mActivePointerId != INVALID_POINTER_ID)
                || (mActivePointerId2 != INVALID_POINTER_ID)) {
              mLastTouchX = x;
              mLastTouchY = y;
            }
          } catch (Exception e) {
            // can be caused by
            // pointerIndex= ev.findPointerIndex(mActivePointerId);
            // x= ev.getX(pointerIndex);
            // above which seems to be a Android bug
          }
          invalidate();
          break;
        }
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
        {
          breakMapThread = true;
          mActivePointerId = INVALID_POINTER_ID;
          mActivePointerId2 = INVALID_POINTER_ID;
          restartMap();
          break;
        }
    }
    return true;
  }