Mega Code Archive

 
Categories / Android / UI
 

Velocity tracker Demo

package app.test; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.VelocityTracker; public class Test extends Activity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);     }     private VelocityTracker vTracker = null;     public boolean onTouchEvent(MotionEvent event) {         int action = event.getAction();         switch(action) {             case MotionEvent.ACTION_DOWN:                 if(vTracker == null) {                     vTracker = VelocityTracker.obtain();                 }                 else {                     vTracker.clear();                 }                 vTracker.addMovement(event);                 break;             case MotionEvent.ACTION_MOVE:                 vTracker.addMovement(event);                 vTracker.computeCurrentVelocity(1000);                 Log.v("", "X velocity is " + vTracker.getXVelocity() +" pixels per second");                 Log.v("", "Y velocity is " + vTracker.getYVelocity() +" pixels per second");                 break;             case MotionEvent.ACTION_UP:             case MotionEvent.ACTION_CANCEL:                 vTracker.recycle();                 break;         }         return true;     } }