{
     velocitytracker = mVelocityTracker;
     k = MotionEventCompat.getPointerId(motionevent, 0);
     velocitytracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
     f = VelocityTrackerCompat.getYVelocity(velocitytracker, k);
     f1 = VelocityTrackerCompat.getXVelocity(velocitytracker, k);
     if (Math.abs(f) > (float)mMinimumFlingVelocity || Math.abs(f1) > (float)mMinimumFlingVelocity)
     {
         flag3 = mListener.mListener(mCurrentDownEvent, motionevent, f1, f);
     } else
     {
         flag3 = false;
     }
 }
예제 #2
0
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    if (!mEnabled) return false;

    //		if (!mIsBeingDragged && !thisTouchAllowed(ev))
    //			return false;

    if (!mIsBeingDragged && !mQuickReturn) return false;

    final int action = ev.getAction();

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
      case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
          if (mActivePointerId == INVALID_POINTER) break;
          final int pointerIndex = getPointerIndex(ev, mActivePointerId);
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float dx = x - mLastMotionX;
          final float xDiff = Math.abs(dx);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float yDiff = Math.abs(y - mLastMotionY);
          if (DEBUG)
            Log.v(
                TAG,
                "onTouch moved to:("
                    + x
                    + ", "
                    + y
                    + "), diff:("
                    + xDiff
                    + ", "
                    + yDiff
                    + ")\nmIsBeingDragged:"
                    + mIsBeingDragged
                    + ", mLastMotionX:"
                    + mLastMotionX);
          if ((xDiff > mTouchSlop || (mQuickReturn && xDiff > mTouchSlop / 4))
              && xDiff > yDiff
              && thisSlideAllowed(dx)) {
            if (DEBUG) Log.v(TAG, "Starting drag! from onTouch");
            startDrag();
            mLastMotionX = x;
            setScrollingCacheEnabled(true);
          } else {
            if (DEBUG) Log.v(TAG, "onTouch returning false");
            return false;
          }
        }
        if (mIsBeingDragged) {
          // Scroll to follow the motion event
          final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
          if (mActivePointerId == INVALID_POINTER) {
            break;
          }
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float deltaX = mLastMotionX - x;
          mLastMotionX = x;
          float oldScrollX = getScrollX();
          float scrollX = oldScrollX + deltaX;
          final float leftBound = getLeftBound();
          final float rightBound = getRightBound();
          if (scrollX < leftBound) {
            scrollX = leftBound;
          } else if (scrollX > rightBound) {
            scrollX = rightBound;
          }
          // Don't lose the rounded component
          mLastMotionX += scrollX - (int) scrollX;
          scrollTo((int) scrollX, getScrollY());
          pageScrolled((int) scrollX);
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          int initialVelocity =
              (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
          final int scrollX = getScrollX();
          //				final int widthWithMargin = getWidth();
          //				final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
          // TODO test this. should get better flinging behavior
          final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
          final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
          if (mActivePointerId != INVALID_POINTER) {
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);
          } else {
            setCurrentItemInternal(mCurItem, true, true, initialVelocity);
          }
          mActivePointerId = INVALID_POINTER;
          endDrag();
        } else if (mQuickReturn
            && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
          // close the menu
          setCurrentItem(1);
          endDrag();
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
          setCurrentItemInternal(mCurItem, true, true);
          mActivePointerId = INVALID_POINTER;
          endDrag();
        }
        break;
      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int index = MotionEventCompat.getActionIndex(ev);
          final float x = MotionEventCompat.getX(ev, index);
          mLastMotionX = x;
          mActivePointerId = MotionEventCompat.getPointerId(ev, index);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        int pointerIndex = this.getPointerIndex(ev, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER) break;
        mLastMotionX = MotionEventCompat.getX(ev, pointerIndex);
        break;
    }
    return true;
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    if (!mEnabled) return false;

    if (!mIsBeingDragged && !thisTouchAllowed(ev)) return false;

    //		if (!mIsBeingDragged && !mQuickReturn)
    //			return false;

    final int action = ev.getAction();

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        mLastMotionX = mInitialMotionX = ev.getX();
        break;
      case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
          determineDrag(ev);
          if (mIsUnableToDrag) return false;
        }
        if (mIsBeingDragged) {
          // Scroll to follow the motion event
          final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
          if (mActivePointerId == INVALID_POINTER) break;
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float deltaX = mLastMotionX - x;
          mLastMotionX = x;
          float oldScrollX = getScrollX();
          float scrollX = oldScrollX + deltaX;
          final float leftBound = getLeftBound();
          final float rightBound = getRightBound();
          if (scrollX < leftBound) {
            scrollX = leftBound;
          } else if (scrollX > rightBound) {
            scrollX = rightBound;
          }
          // Don't lose the rounded component
          mLastMotionX += scrollX - (int) scrollX;
          scrollTo((int) scrollX, getScrollY());
          pageScrolled((int) scrollX);
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          int initialVelocity =
              (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
          final int scrollX = getScrollX();
          final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
          final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
          if (mActivePointerId != INVALID_POINTER) {
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);
          } else {
            setCurrentItemInternal(mCurItem, true, true, initialVelocity);
          }
          mActivePointerId = INVALID_POINTER;
          endDrag();
        } else if (mQuickReturn
            && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
          // close the menu
          setCurrentItem(1);
          endDrag();
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
          setCurrentItemInternal(mCurItem, true, true);
          mActivePointerId = INVALID_POINTER;
          endDrag();
        }
        break;
      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int indexx = MotionEventCompat.getActionIndex(ev);
          mLastMotionX = MotionEventCompat.getX(ev, indexx);
          mActivePointerId = MotionEventCompat.getPointerId(ev, indexx);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        int pointerIndex = getPointerIndex(ev, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER) break;
        mLastMotionX = MotionEventCompat.getX(ev, pointerIndex);
        break;
    }
    return true;
  }
예제 #4
0
        @Override
        public boolean onTouch(final View v, MotionEvent event) {
          int index = event.getActionIndex();
          int pointerId = event.getPointerId(index);

          if (mSwipeSlop < 0) {
            mSwipeSlop = ViewConfiguration.get(getActivity()).getScaledTouchSlop();
          }

          v.onTouchEvent(event);

          switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
              if (mItemPressed) {
                // Multi-item swipes not handled
                return false;
              }
              mItemPressed = true;
              mDownX = event.getX();
              if (mVelocityTracker == null) {
                // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                mVelocityTracker = VelocityTracker.obtain();
              } else {
                // Reset the velocity tracker back to its initial state.
                mVelocityTracker.clear();
              }
              mVelocityTracker.addMovement(event);
              break;
            case MotionEvent.ACTION_CANCEL:
              v.setAlpha(1);
              v.setTranslationX(0);
              mItemPressed = false;
              mVelocityTracker.recycle();
              mVelocityTracker = null;
              break;
            case MotionEvent.ACTION_MOVE:
              {
                mVelocityTracker.addMovement(event);
                float x = event.getX() + v.getTranslationX();
                float deltaX = x - mDownX;
                float deltaXAbs = Math.abs(deltaX);
                if (!mSwiping) {
                  if (deltaXAbs > mSwipeSlop) {
                    mSwiping = true;
                    getListView().requestDisallowInterceptTouchEvent(true);
                    mBackgroundContainer.showBackground(v.getTop(), v.getHeight());
                  }
                }
                if (mSwiping) {
                  v.setTranslationX((x - mDownX));
                  v.setAlpha(1 - deltaXAbs / v.getWidth());
                }
              }
              break;
            case MotionEvent.ACTION_UP:
              {
                // User let go - figure out whether to animate the view out, or back into place
                if (mSwiping) {
                  float x = event.getX() + v.getTranslationX();
                  float deltaX = x - mDownX;
                  float deltaXAbs = Math.abs(deltaX);
                  float fractionCovered;
                  float endX;
                  float endAlpha;
                  final boolean remove;
                  mVelocityTracker.computeCurrentVelocity(1000);
                  float velocityX =
                      Math.abs(VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId));
                  if (velocityX > 700 || deltaXAbs > v.getWidth() / 4) {
                    fractionCovered = deltaXAbs / v.getWidth();
                    endX = deltaX < 0 ? -v.getWidth() : v.getWidth();
                    endAlpha = 0;
                    remove = true;
                  } else {
                    // Not far enough - animate it back
                    fractionCovered = 1 - (deltaXAbs / v.getWidth());
                    endX = 0;
                    endAlpha = 1;
                    remove = false;
                  }
                  mVelocityTracker.clear();
                  int SWIPE_DURATION = 600;
                  long duration = (int) ((1 - fractionCovered) * SWIPE_DURATION);
                  getListView().setEnabled(false);
                  v.animate()
                      .setDuration(Math.abs(duration))
                      .alpha(endAlpha)
                      .translationX(endX)
                      .setListener(
                          new AnimatorActionListener(
                              new Runnable() {
                                @Override
                                public void run() {
                                  // Restore animated values
                                  v.setAlpha(1);
                                  v.setTranslationX(0);
                                  if (remove) {
                                    animateRemoval(getListView(), v);
                                  } else {
                                    mBackgroundContainer.hideBackground();
                                    getListView().setEnabled(true);
                                  }
                                }
                              },
                              AnimatorActionListener.ActionType.END));
                }
              }
              mItemPressed = false;
              break;
            default:
              return false;
          }
          return true;
        }
    public boolean onTouchEvent(MotionEvent motionevent)
    {
        float f;
        float f1;
        int i2;
        int j2 = motionevent.getAction();
        if (mVelocityTracker == null)
        {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(motionevent);
        boolean flag;
        int l;
        int k1;
        if ((j2 & 0xff) == 6)
        {
            flag = true;
        } else
        {
            flag = false;
        }
        if (flag)
        {
            l = MotionEventCompat.getActionIndex(motionevent);
        } else
        {
            l = -1;
        }
        i2 = MotionEventCompat.getPointerCount(motionevent);
        k1 = 0;
        f = 0.0F;
        float f2;
        for (f1 = 0.0F; k1 < i2; f1 = f2)
        {
            float f5 = f;
            f2 = f1;
            if (l != k1)
            {
                f2 = f1 + MotionEventCompat.getX(motionevent, k1);
                f5 = f + MotionEventCompat.getY(motionevent, k1);
            }
            k1++;
            f = f5;
        }

        int i;
        if (flag)
        {
            i = i2 - 1;
        } else
        {
            i = i2;
        }
        f1 /= i;
        f /= i;
        j2 & 0xff;
        JVM INSTR tableswitch 0 6: default 204
    //                   0 383
    //                   1 862
    //                   2 643
    //                   3 1136
    //                   4 204
    //                   5 213
    //                   6 239;
           goto _L1 _L2 _L3 _L4 _L5 _L1 _L6 _L7
_L1:
        return false;
_L6:
        mLastFocusX = f1;
        mDownFocusX = f1;
        mLastFocusY = f;
        mDownFocusY = f;
        cancelTaps();
        return false;
_L7:
        mLastFocusX = f1;
        mDownFocusX = f1;
        mLastFocusY = f;
        mDownFocusY = f;
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
        int i1 = MotionEventCompat.getActionIndex(motionevent);
        int j = MotionEventCompat.getPointerId(motionevent, i1);
        f = VelocityTrackerCompat.getXVelocity(mVelocityTracker, j);
        f1 = VelocityTrackerCompat.getYVelocity(mVelocityTracker, j);
        j = 0;
        while (j < i2) 
        {
            if (j != i1)
            {
                int l1 = MotionEventCompat.getPointerId(motionevent, j);
                float f3 = VelocityTrackerCompat.getXVelocity(mVelocityTracker, l1);
                if (VelocityTrackerCompat.getYVelocity(mVelocityTracker, l1) * f1 + f3 * f < 0.0F)
                {
                    mVelocityTracker.clear();
                    return false;
                }
            }
            j++;
        }
          goto _L8
  @Override
  public boolean onTouch(MotionEvent e) {
    switch (e.getAction()) {
      case MotionEvent.ACTION_DOWN:
        if (gamestate == State.ROUNDSUMMARY
            || gamestate == State.STARTGAME
            || gamestate == State.PLAYERDIED) {
          // Game�̏�����
          randomSpeed = 1;
          gamestate = State.STARTROUND; // prep and start round
          return false; // no followup msgs
        } else if (gamestate == State.GAMEOVER) {
          act.leaveGame(); // user touched after gameover -> back to entry screen
          return false; // no followup msgs
        } else {
          synchronized (fruitsSelectable) {
            Iterator<Fruit> itf = fruitsSelectable.iterator();
            while (itf.hasNext()) {
              Fruit f = itf.next();
              if (f.hasCollision(e.getX(), e.getY()))
                if (f.seed == ketseed) {
                  // user popped ketchup
                  act.playSound(Sound.KSPLAT);
                  f.burst();
                  loseLife();
                  return false; // no followup msgs
                } else {
                  // user picked up a fruit
                  selectedFruit = f;
                }
            }
          }
          if (mVelocityTracker == null) {
            // Retrieve a new VelocityTracker object to watch the velocity of a motion.
            mVelocityTracker = VelocityTracker.obtain();
          } else {
            // Reset the velocity tracker back to its initial state.
            mVelocityTracker.clear();
          }
          // Add a user's movement to the tracker.
          mVelocityTracker.addMovement(e);
        }
        break;

      case MotionEvent.ACTION_MOVE:
        if (selectedFruit != null) {
          selectedFruit.x = e.getX();
          selectedFruit.y = e.getY();
        }
        mVelocityTracker.addMovement(e);
        break;

      case MotionEvent.ACTION_UP:
        if (selectedFruit != null) {
          Fruit f = selectedFruit;
          selectedFruit = null;

          mVelocityTracker.computeCurrentVelocity(1000);
          int pointerId = e.getPointerId(e.getActionIndex());
          float tvx = VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
          float tvy = VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId);

          if (-tvy > 10) {
            // there is upward motion at release-- user threw fruit

            // scale throw speed for display size/density
            tvx = tvx / act.densityscalefactor;
            tvy = tvy / act.densityscalefactor;

            // help ease perspective problem when we release fruit away from the horizontal center
            // of the screen
            tvx += (e.getX() - width / 2) * 3.5 * act.densityscalefactor;

            f.throwFruit(tvx, tvy);
            synchronized (fruitsFlying) {
              fruitsFlying.add(f);
              fruitsSelectable.remove(f);
            }

            // attempting to adjust sound for how hard fruit was thrown horizontally.
            // hardness == 0 --> not thrown with any force
            // hardness == 1 --> thrown as hard as possible
            // assume that 5000 represents "really fast"; z vel should sound "harder" than y-vel
            float hardness = (f.vz - f.vy / 2) / 5000; // vy: up is negative.
            if (hardness >= 1f) hardness = 1.0f;
            if (hardness < .3f) hardness = .3f;
            act.playSound(Sound.THROW, hardness * .9f, hardness * 2);
          }
        }
        mVelocityTracker.recycle();
        // seems to be a bug here on android 4.4, causing IllegalStateException - not addressing,
        // since it doesn't affect game play, and may be resolved in later versions
        break;
    }

    return true;
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (!mEnabled
        || !mIsDragging
            && !mLastTouchAllowed
            && !allowSlidingFromHereX(ev, mInitialX)
            && !allowSlidingFromHereY(ev, mInitialY)) {
      return false;
    }

    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_UP
        || action == MotionEvent.ACTION_CANCEL
        || action == MotionEvent.ACTION_OUTSIDE) {
      mLastTouchAllowed = false;
    } else {
      mLastTouchAllowed = true;
    }

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        completeScroll();

        // Remember where the motion event started
        mLastX = mInitialX = ev.getX();
        mLastY = mInitialY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
      case MotionEvent.ACTION_MOVE:
        if (!mIsDragging) {
          final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (pointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
          }
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float xDiff = Math.abs(x - mLastX);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float yDiff = Math.abs(y - mLastY);
          if (xDiff > mTouchSlop && xDiff > yDiff) {
            mIsDragging = true;
            mLastX = x;
            setDrawingCacheEnabled(true);
          } else if (yDiff > mTouchSlop && yDiff > xDiff) {
            mIsDragging = true;
            mLastY = y;
            setDrawingCacheEnabled(true);
          }
        }
        if (mIsDragging) {
          // Scroll to follow the motion event
          final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (activePointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
          }
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float y = MotionEventCompat.getY(ev, activePointerIndex);
          final float deltaX = mLastX - x;
          final float deltaY = mLastY - y;
          mLastX = x;
          mLastY = y;
          final float oldScrollX = getScrollX();
          final float oldScrollY = getScrollY();
          float scrollX = oldScrollX + deltaX;
          float scrollY = oldScrollY + deltaY;

          // Log.d("Layer", String.format("Layer scrollX[%f],scrollY[%f]", scrollX, scrollY));
          final float leftBound, rightBound;
          final float bottomBound, topBound;
          switch (mScreenSide) {
            case STICK_TO_LEFT:
              topBound = bottomBound = rightBound = 0;
              leftBound = getWidth(); // How far left we can scroll
              break;
            case STICK_TO_MIDDLE:
              topBound = getHeight();
              bottomBound = -getHeight();
              leftBound = getWidth();
              rightBound = -getWidth();
              break;
            case STICK_TO_RIGHT:
              rightBound = -getWidth();
              topBound = bottomBound = leftBound = 0;
              break;
            case STICK_TO_TOP:
              topBound = getHeight();
              bottomBound = rightBound = leftBound = 0;
              break;
            case STICK_TO_BOTTOM:
              topBound = rightBound = leftBound = 0;
              bottomBound = -getHeight();
              break;
            default:
              topBound = bottomBound = rightBound = leftBound = 0;
              break;
          }
          if (scrollX > leftBound) {
            scrollX = leftBound;
          } else if (scrollX < rightBound) {
            scrollX = rightBound;
          }
          if (scrollY > topBound) {
            scrollY = topBound;
          } else if (scrollY < bottomBound) {
            scrollY = bottomBound;
          }

          // Keep the precision
          mLastX += scrollX - (int) scrollX;
          mLastY += scrollY - (int) scrollY;
          scrollTo((int) scrollX, (int) scrollY);
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsDragging) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          final int initialVelocityX =
              (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
          final int initialVelocityY =
              (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
          final int scrollX = getScrollX();
          final int scrollY = getScrollY();
          final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float y = MotionEventCompat.getY(ev, activePointerIndex);
          final int totalDeltaX = (int) (x - mInitialX);
          final int totalDeltaY = (int) (y - mInitialY);

          boolean nextStateOpened =
              determineNextStateOpened(
                  mIsOpen,
                  scrollX,
                  scrollY,
                  initialVelocityX,
                  initialVelocityY,
                  totalDeltaX,
                  totalDeltaY);
          switchLayer(nextStateOpened, true, true, initialVelocityX, initialVelocityY);

          mActivePointerId = INVALID_POINTER;
          endDrag();
        } else if (mIsOpen && closeOnTapEnabled) {
          closeLayer(true);
        } else if (!mIsOpen && openOnTapEnabled) {
          openLayer(true);
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsDragging) {
          switchLayer(mIsOpen, true, true);
          mActivePointerId = INVALID_POINTER;
          endDrag();
        }
        break;
      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int index = MotionEventCompat.getActionIndex(ev);
          mLastX = MotionEventCompat.getX(ev, index);
          mLastY = MotionEventCompat.getY(ev, index);
          mActivePointerId = MotionEventCompat.getPointerId(ev, index);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastX =
            MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        mLastY =
            MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (mActivePointerId == INVALID_POINTER) {
      mLastTouchAllowed = false;
    }
    return true;
  }
예제 #8
0
파일: FlipView.java 프로젝트: Y-bao/Android
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    if (!mIsFlippingEnabled) {
      return false;
    }

    if (mPageCount < 1) {
      return false;
    }

    if (!mIsFlipping && !mLastTouchAllowed) {
      return false;
    }

    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_UP
        || action == MotionEvent.ACTION_CANCEL
        || action == MotionEvent.ACTION_OUTSIDE) {
      mLastTouchAllowed = false;
    } else {
      mLastTouchAllowed = true;
    }

    trackVelocity(ev);

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:

        // start flipping immediately if interrupting some sort of animation
        if (endScroll() || endPeak()) {
          mIsFlipping = true;
        }

        // Remember where the motion event started
        mLastX = ev.getX();
        mLastY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
      case MotionEvent.ACTION_MOVE:
        if (!mIsFlipping) {
          final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (pointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
          }
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float xDiff = Math.abs(x - mLastX);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float yDiff = Math.abs(y - mLastY);
          if ((mIsFlippingVertically && yDiff > mTouchSlop && yDiff > xDiff)
              || (!mIsFlippingVertically && xDiff > mTouchSlop && xDiff > yDiff)) {
            mIsFlipping = true;
            mLastX = x;
            mLastY = y;
          }
        }
        if (mIsFlipping) {
          // Scroll to follow the motion event
          final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (activePointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
          }
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float deltaX = mLastX - x;
          final float y = MotionEventCompat.getY(ev, activePointerIndex);
          final float deltaY = mLastY - y;
          mLastX = x;
          mLastY = y;

          float deltaFlipDistance = 0;
          if (mIsFlippingVertically) {
            deltaFlipDistance = deltaY;
          } else {
            deltaFlipDistance = deltaX;
          }

          deltaFlipDistance /=
              ((isFlippingVertically() ? getHeight() : getWidth()) / FLIP_DISTANCE_PER_PAGE);
          setFlipDistance(mFlipDistance + deltaFlipDistance);

          final int minFlipDistance = 0;
          final int maxFlipDistance = (mPageCount - 1) * FLIP_DISTANCE_PER_PAGE;
          final boolean isOverFlipping =
              mFlipDistance < minFlipDistance || mFlipDistance > maxFlipDistance;
          if (isOverFlipping) {
            mIsOverFlipping = true;
            setFlipDistance(
                mOverFlipper.calculate(mFlipDistance, minFlipDistance, maxFlipDistance));
            if (mOnOverFlipListener != null) {
              float overFlip = mOverFlipper.getTotalOverFlip();
              mOnOverFlipListener.onOverFlip(
                  this, mOverFlipMode, overFlip < 0, Math.abs(overFlip), FLIP_DISTANCE_PER_PAGE);
            }
          } else if (mIsOverFlipping) {
            mIsOverFlipping = false;
            if (mOnOverFlipListener != null) {
              // TODO in the future should only notify flip distance 0
              // on the correct edge (previous/next)
              mOnOverFlipListener.onOverFlip(this, mOverFlipMode, false, 0, FLIP_DISTANCE_PER_PAGE);
              mOnOverFlipListener.onOverFlip(this, mOverFlipMode, true, 0, FLIP_DISTANCE_PER_PAGE);
            }
          }
        }
        break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
        if (mIsFlipping) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

          int velocity = 0;
          if (isFlippingVertically()) {
            velocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
          } else {
            velocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
          }
          smoothFlipTo(getNextPage(velocity));

          mActivePointerId = INVALID_POINTER;
          endFlip();

          mOverFlipper.overFlipEnded();
        }
        break;
      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int index = MotionEventCompat.getActionIndex(ev);
          final float x = MotionEventCompat.getX(ev, index);
          final float y = MotionEventCompat.getY(ev, index);
          mLastX = x;
          mLastY = y;
          mActivePointerId = MotionEventCompat.getPointerId(ev, index);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, index);
        final float y = MotionEventCompat.getY(ev, index);
        mLastX = x;
        mLastY = y;
        break;
    }
    if (mActivePointerId == INVALID_POINTER) {
      mLastTouchAllowed = false;
    }
    return true;
  }
  @Override
  public boolean onTouchEvent(MotionEvent e) {
    /*
    if (mLayoutFrozen || mIgnoreMotionEventTillDown) {
        return false;
    }
    if (dispatchOnItemTouch(e)) {
        cancelTouch();
        return true;
    }

    if (mLayout == null) {
        return false;
    }
    */

    final boolean canScrollHorizontally = false; // mLayout.canScrollHorizontally();
    final boolean canScrollVertically = true; // mLayout.canScrollVertically();

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    boolean eventAddedToVelocityTracker = false;

    final MotionEvent vtev = MotionEvent.obtain(e);
    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    if (action == MotionEvent.ACTION_DOWN) {
      mNestedOffsets[0] = mNestedOffsets[1] = 0;
    }
    vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]);

    switch (action) {
      case MotionEvent.ACTION_DOWN:
        {
          mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
          mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);
          mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);

          int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE;
          if (canScrollHorizontally) {
            nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL;
          }
          if (canScrollVertically) {
            nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL;
          }
          startNestedScroll(nestedScrollAxis);
        }
        break;

      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
          mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
          mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
        }
        break;

      case MotionEvent.ACTION_MOVE:
        {
          final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
          if (index < 0) {
            Log.e(
                TAG,
                "Error processing scroll; pointer index for id "
                    + mScrollPointerId
                    + " not found. Did any MotionEvents get skipped?");
            return false;
          }

          final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
          final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
          int dx = mLastTouchX - x;
          int dy = mLastTouchY - y;

          if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) {
            dx -= mScrollConsumed[0];
            dy -= mScrollConsumed[1];
            vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
            // Updated the nested offsets
            mNestedOffsets[0] += mScrollOffset[0];
            mNestedOffsets[1] += mScrollOffset[1];
          }

          if (mScrollState != SCROLL_STATE_DRAGGING) {
            boolean startScroll = false;
            if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) {
              if (dx > 0) {
                dx -= mTouchSlop;
              } else {
                dx += mTouchSlop;
              }
              startScroll = true;
            }
            if (canScrollVertically && Math.abs(dy) > mTouchSlop) {
              if (dy > 0) {
                dy -= mTouchSlop;
              } else {
                dy += mTouchSlop;
              }
              startScroll = true;
            }
            if (startScroll) {
              setScrollState(SCROLL_STATE_DRAGGING);
            }
          }

          if (mScrollState == SCROLL_STATE_DRAGGING) {
            mLastTouchX = x - mScrollOffset[0];
            mLastTouchY = y - mScrollOffset[1];

            if (scrollByInternal(
                canScrollHorizontally ? dx : 0, canScrollVertically ? dy : 0, vtev)) {
              getParent().requestDisallowInterceptTouchEvent(true);
            }
          }
        }
        break;

      case MotionEventCompat.ACTION_POINTER_UP:
        {
          onPointerUp(e);
        }
        break;

      case MotionEvent.ACTION_UP:
        {
          mVelocityTracker.addMovement(vtev);
          eventAddedToVelocityTracker = true;
          mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
          final float xvel =
              canScrollHorizontally
                  ? -VelocityTrackerCompat.getXVelocity(mVelocityTracker, mScrollPointerId)
                  : 0;
          final float yvel =
              canScrollVertically
                  ? -VelocityTrackerCompat.getYVelocity(mVelocityTracker, mScrollPointerId)
                  : 0;
          if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) {
            setScrollState(SCROLL_STATE_IDLE);
          }
          resetTouch();
        }
        break;

      case MotionEvent.ACTION_CANCEL:
        {
          cancelTouch();
        }
        break;
    }

    if (!eventAddedToVelocityTracker) {
      mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();

    return true;
  }
예제 #10
0
  public boolean onTouchEvent(MotionEvent var1) {
    this.initVelocityTrackerIfNotExists();
    MotionEvent var9 = MotionEvent.obtain(var1);
    int var2 = MotionEventCompat.getActionMasked(var1);
    if (var2 == 0) {
      this.mNestedYOffset = 0;
    }

    var9.offsetLocation(0.0F, (float) this.mNestedYOffset);
    ViewParent var10;
    switch (var2) {
      case 0:
        if (this.getChildCount() == 0) {
          return false;
        }

        boolean var8;
        if (!this.mScroller.isFinished()) {
          var8 = true;
        } else {
          var8 = false;
        }

        this.mIsBeingDragged = var8;
        if (var8) {
          var10 = this.getParent();
          if (var10 != null) {
            var10.requestDisallowInterceptTouchEvent(true);
          }
        }

        if (!this.mScroller.isFinished()) {
          this.mScroller.abortAnimation();
        }

        this.mLastMotionY = (int) var1.getY();
        this.mActivePointerId = MotionEventCompat.getPointerId(var1, 0);
        this.startNestedScroll(2);
        break;
      case 1:
        if (this.mIsBeingDragged) {
          VelocityTracker var11 = this.mVelocityTracker;
          var11.computeCurrentVelocity(1000, (float) this.mMaximumVelocity);
          var2 = (int) VelocityTrackerCompat.getYVelocity(var11, this.mActivePointerId);
          if (Math.abs(var2) > this.mMinimumVelocity) {
            this.flingWithNestedDispatch(-var2);
          }

          this.mActivePointerId = -1;
          this.endDrag();
        }
        break;
      case 2:
        int var4 = MotionEventCompat.findPointerIndex(var1, this.mActivePointerId);
        if (var4 == -1) {
          Log.e(
              "NestedScrollView",
              "Invalid pointerId=" + this.mActivePointerId + " in onTouchEvent");
        } else {
          int var5 = (int) MotionEventCompat.getY(var1, var4);
          var2 = this.mLastMotionY - var5;
          int var3 = var2;
          if (this.dispatchNestedPreScroll(0, var2, this.mScrollConsumed, this.mScrollOffset)) {
            var3 = var2 - this.mScrollConsumed[1];
            var9.offsetLocation(0.0F, (float) this.mScrollOffset[1]);
            this.mNestedYOffset += this.mScrollOffset[1];
          }

          var2 = var3;
          if (!this.mIsBeingDragged) {
            var2 = var3;
            if (Math.abs(var3) > this.mTouchSlop) {
              var10 = this.getParent();
              if (var10 != null) {
                var10.requestDisallowInterceptTouchEvent(true);
              }

              this.mIsBeingDragged = true;
              if (var3 > 0) {
                var2 = var3 - this.mTouchSlop;
              } else {
                var2 = var3 + this.mTouchSlop;
              }
            }
          }

          if (this.mIsBeingDragged) {
            this.mLastMotionY = var5 - this.mScrollOffset[1];
            int var6 = this.getScrollY();
            var5 = this.getScrollRange();
            var3 = ViewCompat.getOverScrollMode(this);
            boolean var12;
            if (var3 != 0 && (var3 != 1 || var5 <= 0)) {
              var12 = false;
            } else {
              var12 = true;
            }

            if (this.overScrollByCompat(0, var2, 0, this.getScrollY(), 0, var5, 0, 0, true)
                && !this.hasNestedScrollingParent()) {
              this.mVelocityTracker.clear();
            }

            int var7 = this.getScrollY() - var6;
            if (this.dispatchNestedScroll(0, var7, 0, var2 - var7, this.mScrollOffset)) {
              this.mLastMotionY -= this.mScrollOffset[1];
              var9.offsetLocation(0.0F, (float) this.mScrollOffset[1]);
              this.mNestedYOffset += this.mScrollOffset[1];
            } else if (var12) {
              this.ensureGlows();
              var3 = var6 + var2;
              if (var3 < 0) {
                this.mEdgeGlowTop.onPull(
                    (float) var2 / (float) this.getHeight(),
                    MotionEventCompat.getX(var1, var4) / (float) this.getWidth());
                if (!this.mEdgeGlowBottom.isFinished()) {
                  this.mEdgeGlowBottom.onRelease();
                }
              } else if (var3 > var5) {
                this.mEdgeGlowBottom.onPull(
                    (float) var2 / (float) this.getHeight(),
                    1.0F - MotionEventCompat.getX(var1, var4) / (float) this.getWidth());
                if (!this.mEdgeGlowTop.isFinished()) {
                  this.mEdgeGlowTop.onRelease();
                }
              }

              if (this.mEdgeGlowTop != null
                  && (!this.mEdgeGlowTop.isFinished() || !this.mEdgeGlowBottom.isFinished())) {
                ViewCompat.postInvalidateOnAnimation(this);
              }
            }
          }
        }
        break;
      case 3:
        if (this.mIsBeingDragged && this.getChildCount() > 0) {
          this.mActivePointerId = -1;
          this.endDrag();
        }
      case 4:
      default:
        break;
      case 5:
        var2 = MotionEventCompat.getActionIndex(var1);
        this.mLastMotionY = (int) MotionEventCompat.getY(var1, var2);
        this.mActivePointerId = MotionEventCompat.getPointerId(var1, var2);
        break;
      case 6:
        this.onSecondaryPointerUp(var1);
        this.mLastMotionY =
            (int)
                MotionEventCompat.getY(
                    var1, MotionEventCompat.findPointerIndex(var1, this.mActivePointerId));
    }

    if (this.mVelocityTracker != null) {
      this.mVelocityTracker.addMovement(var9);
    }

    var9.recycle();
    return true;
  }