コード例 #1
0
    void fling(int start, int velocity, int min, int max, int over) {
      mOver = over;
      mFinished = false;
      mCurrVelocity = mVelocity = velocity;
      mDuration = mSplineDuration = 0;
      mStartTime = AnimationUtils.currentAnimationTimeMillis();
      mCurrentPosition = mStart = start;

      if (start > max || start < min) {
        startAfterEdge(start, min, max, velocity);
        return;
      }

      mState = SPLINE;
      double totalDistance = 0.0;

      if (velocity != 0) {
        mDuration = mSplineDuration = getSplineFlingDuration(velocity);
        totalDistance = getSplineFlingDistance(velocity);
        mPm.cpuBoost(mDuration * 1000);
      }

      mSplineDistance = (int) (totalDistance * Math.signum(velocity));
      mFinal = start + mSplineDistance;

      // Clamp to a valid final position
      if (mFinal < min) {
        adjustDuration(mStart, mFinal, min);
        mFinal = min;
      }

      if (mFinal > max) {
        adjustDuration(mStart, mFinal, max);
        mFinal = max;
      }
    }
コード例 #2
0
  public boolean onTouchEvent(MotionEvent ev) {
    if (mInEditMode) {
      return false;
    }
    final int action = ev.getAction();
    int x, y;

    // A lot of stuff is about to happen. Lets get ready.
    mPm.cpuBoost(750000);

    switch (action) {
      case MotionEvent.ACTION_DOWN:
        // Log.d("KeyButtonView", "press");
        mDownTime = SystemClock.uptimeMillis();
        setPressed(true);
        if (mCode == KeyEvent.KEYCODE_DPAD_LEFT || mCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
          sendEvent(
              KeyEvent.ACTION_DOWN,
              KeyEvent.FLAG_VIRTUAL_HARD_KEY | KeyEvent.FLAG_KEEP_TOUCH_MODE,
              mDownTime,
              false);
        } else if (mCode != 0) {
          sendEvent(KeyEvent.ACTION_DOWN, 0, mDownTime);
        } else {
          // Provide the same haptic feedback that the system offers for virtual keys.
          performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        }
        if (supportsLongPress()) {
          removeCallbacks(mCheckLongPress);
          postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout());
        }
        break;
      case MotionEvent.ACTION_MOVE:
        x = (int) ev.getX();
        y = (int) ev.getY();
        setPressed(
            x >= -mTouchSlop
                && x < getWidth() + mTouchSlop
                && y >= -mTouchSlop
                && y < getHeight() + mTouchSlop);
        break;
      case MotionEvent.ACTION_CANCEL:
        setPressed(false);
        if (mCode != 0) {
          sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED);
        }
        if (supportsLongPress()) {
          removeCallbacks(mCheckLongPress);
        }
        break;
      case MotionEvent.ACTION_UP:
        final boolean doIt = isPressed();
        setPressed(false);
        if (mCode != 0) {
          if (doIt) {
            sendEvent(KeyEvent.ACTION_UP, 0);
            sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
            playSoundEffect(SoundEffectConstants.CLICK);
          } else {
            sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED);
          }
        } else {
          // no key code, just a regular ImageView
          if (doIt && !mPerformedLongClick) {
            performClick();
          }
        }
        if (supportsLongPress()) {
          removeCallbacks(mCheckLongPress);
        }
        mPerformedLongClick = false;
        break;
    }

    mHandler.post(mNavButtonDimActivator);

    return true;
  }