Code example for VelocityTracker

Methods: addMovementcomputeCurrentVelocitygetXVelocityrecycle

0
		} 
		 
		if (mVelocityTracker == null) {
			mVelocityTracker = VelocityTracker.obtain();
		} 
		mVelocityTracker.addMovement(event);
		final int action = event.getAction();
		final float x = event.getX();
		final float y = event.getY();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			//Log.e(TAG, "event down!"); 
			if (!mScroller.isFinished()) {
				mScroller.abortAnimation();
			} 
			mLastMotionX = x;
			 
			//---------------New Code---------------------- 
			mLastMotionY = y;
			//--------------------------------------------- 
			 
			break; 
		case MotionEvent.ACTION_MOVE:
			int deltaX = (int) (mLastMotionX - x);
			 
			//---------------New Code---------------------- 
			int deltaY = (int) (mLastMotionY - y);
			if(Math.abs(deltaX) < 200 && Math.abs(deltaY) > 10)
				break; 
			mLastMotionY = y;
			//------------------------------------- 
			 
			mLastMotionX = x;
			scrollBy(deltaX, 0);
			break; 
		case MotionEvent.ACTION_UP:
			//Log.e(TAG, "event : up"); 
			// if (mTouchState == TOUCH_STATE_SCROLLING) { 
			final VelocityTracker velocityTracker = mVelocityTracker;
			velocityTracker.computeCurrentVelocity(1000);
			int velocityX = (int) velocityTracker.getXVelocity();
			//Log.e(TAG, "velocityX:" + velocityX); 
			if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
				// Fling enough to move left 
				//Log.e(TAG, "snap left"); 
				snapToScreen(mCurScreen - 1);
			} else if (velocityX < -SNAP_VELOCITY
					&& mCurScreen < getChildCount() - 1) {
				// Fling enough to move right 
				//Log.e(TAG, "snap right"); 
				snapToScreen(mCurScreen + 1);
			} else { 
				snapToDestination(); 
			} 
			if (mVelocityTracker != null) {
				mVelocityTracker.recycle();
				mVelocityTracker = null;
			} 
			// } 
			mTouchState = TOUCH_STATE_REST;
			break;