Esempio n. 1
1
 // Returns the distance that over the scroll limit.
 public int startScroll(int distance, int min, int max) {
   int currPosition = mScroller.getCurrX();
   int finalPosition = mScroller.isFinished() ? currPosition : mScroller.getFinalX();
   int newPosition = Utils.clamp(finalPosition + distance, min, max);
   if (newPosition != currPosition) {
     mScroller.startScroll(currPosition, 0, newPosition - currPosition, 0, 0);
   }
   return finalPosition + distance - newPosition;
 }
Esempio n. 2
0
 public boolean isFinished() {
   if (isPreGingerbread) {
     return scroller.isFinished();
   } else {
     return overScroller.isFinished();
   }
 }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(event);

    float currentTouchY = event.getY();
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        if (!mOverScroller.isFinished()) {
          mOverScroller.abortAnimation();
        }

        mLastTouchY = currentTouchY;
        break;
      case MotionEvent.ACTION_MOVE:
        float differentY = currentTouchY - mLastTouchY;
        mLastTouchY = currentTouchY;
        if (Math.abs(differentY) > 0) {
          scrollBy(0, (int) -differentY);
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        recycleVelocityTracker();
        if (!mOverScroller.isFinished()) {
          mOverScroller.abortAnimation();
        }
        break;
      case MotionEvent.ACTION_UP:
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        int initialVelocity = (int) mVelocityTracker.getYVelocity();
        if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
          fling(-initialVelocity);
        }
        recycleVelocityTracker();
        break;
    }
    return true;
  }
  private boolean touchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        {
          if (getChildCount() == 0 || !inScrollArea(ev)) {
            return false;
          }
          if ((mIsBeingDragged = !mScroller.isFinished())) {
            final ViewParent parent = getParent();
            if (parent != null) {
              parent.requestDisallowInterceptTouchEvent(true);
            }
          }

          /*
           * If being flinged and user touches, stop the fling. isFinished will be false
           * if being flinged.
           */
          if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
          }

          // Remember where the motion event started
          mLastMotionX = (int) ev.getX();
          mActivePointerId = ev.getPointerId(0);
          break;
        }
      case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        if (activePointerIndex == -1) {
          Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
          break;
        }

        final int x = (int) ev.getX(activePointerIndex);
        int deltaX = mLastMotionX - x;
        if (!mIsBeingDragged && Math.abs(deltaX) > mTouchSlop) {
          final ViewParent parent = getParent();
          if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
          }
          mIsBeingDragged = true;
          if (deltaX > 0) {
            deltaX -= mTouchSlop;
          } else {
            deltaX += mTouchSlop;
          }
        }
        if (mIsBeingDragged) {
          // Scroll to follow the motion event
          mLastMotionX = x;

          final int oldX = getScrollX();
          final int oldY = getScrollY();
          final int range = getScrollRange();
          final int overscrollMode = getOverScrollMode();
          final boolean canOverscroll =
              overscrollMode == OVER_SCROLL_ALWAYS
                  || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);

          if (overScrollBy(deltaX, 0, getScrollX(), 0, range, 0, mOverscrollDistance, 0, true)) {
            // Break our velocity if we hit a scroll barrier.
            mVelocityTracker.clear();
          }
          onScrollChanged(getScrollX(), getScrollY(), oldX, oldY);
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          int initialVelocity = (int) velocityTracker.getXVelocity(mActivePointerId);

          if (getChildCount() > 0) {
            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
              fling(-initialVelocity);
            } else {
              int finalX = getScrollX();
              finalX = getScrollXFromFinalX(finalX);
              if (mScroller.springBack(getScrollX(), getScrollY(), finalX, finalX, 0, 0)) {
                postInvalidateOnAnimation();
              }
            }
          }

          mActivePointerId = INVALID_POINTER;
          mIsBeingDragged = false;
          recycleVelocityTracker();
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
          if (mScroller.springBack(getScrollX(), getScrollY(), 0, getScrollRange(), 0, 0)) {
            postInvalidateOnAnimation();
          }
          mActivePointerId = INVALID_POINTER;
          mIsBeingDragged = false;
          recycleVelocityTracker();
        }
        break;
      case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }
    return true;
  }
 @Override
 public boolean isFinished() {
   return mScroller.isFinished();
 }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // return super.onTouchEvent(event);

    // int height = getHeight();

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(event);

    int action = event.getActionMasked();

    if (action == MotionEvent.ACTION_DOWN) {
      if (!mIsDown) {
        mIsDown = true;
        mDownPt.x = event.getX();
        mDownPt.y = event.getY();
        mDownedScrollPt.x = getScrollX();
        mDownedScrollPt.y = getScrollY();
        // initOrResetVelocityTracker();
        // mVelocityTracker.addMovement(event);

        if (!mScroller.isFinished()) {
          mScroller.abortAnimation();
        }
      }
    } else if (action == MotionEvent.ACTION_MOVE) {
      if (mIsDown) {
        float x = event.getX();
        float y = event.getY();
        float sx = -(x - mDownPt.x) + mDownedScrollPt.x;
        float sy = -(y - mDownPt.y) + mDownedScrollPt.y;
        scrollTo((int) sx, (int) sy);
        //                initVelocityTrackerIfNotExists();
        //                mVelocityTracker.addMovement(event);
      }
    } else if (action == MotionEvent.ACTION_UP) {
      if (mIsDown) {

        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        int initialVelocity = (int) velocityTracker.getYVelocity(event.getPointerId(0));
        int initialVelocitx = (int) velocityTracker.getXVelocity(event.getPointerId(0));
        fling(-initialVelocitx, -initialVelocity);

        recycleVelocityTracker();
        mIsDown = false;
      }
    } else if (action == MotionEvent.ACTION_CANCEL) {
      if (mIsDown) {
        if (mScroller.springBack(
            getScrollX(), getScrollY(), 0, getScrollRangeX(), 0, getScrollRangeY())) {
          // postInvalidateOnAnimation();
          postInvalidate();
        }
        recycleVelocityTracker();
        mIsDown = false;
      }
    }

    return true;
  }
 /** Stops the scroller and any current fling. */
 void stopScroller() {
   Log.d(TAG, "stopScroller: ");
   if (!mScroller.isFinished()) {
     mScroller.abortAnimation();
   }
 }
 /** Returns whether the overscroller is scrolling. */
 boolean isScrolling() {
   Log.d(TAG, "isScrolling: ");
   return !mScroller.isFinished();
 }