@Override public boolean onTouchEvent(MotionEvent event) { if (velocityTracker == null) { // If we do not have velocity tracker velocityTracker = VelocityTracker.obtain(); // then get one } velocityTracker.addMovement(event); // add this movement to it positiveScroll = true; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { if (!flinger.isFinished()) { // If scrolling, then stop now flinger.forceFinished(); } currentX = (int) event.getRawX(); currentY = (int) event.getRawY(); break; } case MotionEvent.ACTION_MOVE: { final int x2 = (int) event.getRawX(); final int y2 = (int) event.getRawY(); final int diffX = currentX - x2; final int diffY = currentY - y2; currentX = x2; currentY = y2; scrollBy(diffX, diffY); break; } case MotionEvent.ACTION_UP: { final VelocityTracker velocityTracker = this.velocityTracker; velocityTracker.computeCurrentVelocity(1000, maximumVelocity); int velocityX = (int) velocityTracker.getXVelocity(); int velocityY = (int) velocityTracker.getYVelocity(); if (Math.abs(velocityX) > minimumVelocity || Math.abs(velocityY) > minimumVelocity) { flinger.start( getActualScrollX(), getActualScrollY(), velocityX, velocityY, getMaxScrollX(), getMaxScrollY()); } else { if (this.velocityTracker != null) { // If the velocity less than threshold this.velocityTracker.recycle(); // recycle the tracker this.velocityTracker = null; } } break; } } return true; }