Code example for VelocityTracker

Methods: addMovementclearcomputeCurrentVelocitygetXVelocityrecycle

0
		int x = (int) event.getX();
		if (mVelocityTracker == null) 
		{ 
			mVelocityTracker = VelocityTracker.obtain();//获得VelocityTracker类实例
		} 
		mVelocityTracker.addMovement(event);//将事件加入到VelocityTracker类实例中
		switch (event.getAction()) 
		{ 
			case MotionEvent.ACTION_DOWN:
				Log.d("wwwwwwwwww","MotionEvent.ACTION_DOWN");
				mLastMotionX = x;
				break; 
			case MotionEvent.ACTION_MOVE:
			{ 
				counti++;
				Log.d("wwwwwwwwww","MotionEvent.ACTION_MOVE");
				mVelocityTracker.computeCurrentVelocity(1000, 1000); 
				//Log.i("test","velocityTraker :"+mVelocityTracker.getXVelocity()); 
				int dx = x - mLastMotionX;
				if(dx != 0)
				{ 
					doRotate(dx);
					if(degree > 90)
					{			 
						degree = 0;		
						break; 
					} 
				} 
				else 
				{ 
					return false; 
				} 
				mLastMotionX = x;
				break; 
			} 
			case MotionEvent.ACTION_UP:
			{ 
				Log.d("wwwwwwwwww","MotionEvent.ACTION_UP");
				//设置units的值为1000,意思为一秒时间内运动了多少个像素 
				mVelocityTracker.computeCurrentVelocity(1000); 
				float VelocityX = mVelocityTracker.getXVelocity();
				//Log.i("test","velocityTraker2:"+mVelocityTracker.getXVelocity()); 
				if(VelocityX > 500 || VelocityX < -500 )
				{ 
					endRotateByVelocity(); 
				} 
				else 
				{ 
					endRotate(); 
				} 
				 
				releaseVelocityTracker();   
               break;   
			} 
           case MotionEvent.ACTION_CANCEL:  
               releaseVelocityTracker();   
               break;   
		}   
		 
		return true; 
	} 
	 
	private void releaseVelocityTracker()  
	{ 
		if(null != mVelocityTracker) {  
            mVelocityTracker.clear();  
            mVelocityTracker.recycle();  
            mVelocityTracker = null;  
        }   
		 
	}