Skip to content

Instantly share code, notes, and snippets.

@shekaroppo
Created July 18, 2015 07:04
Show Gist options
  • Save shekaroppo/13460526894d7e2e2397 to your computer and use it in GitHub Desktop.
Save shekaroppo/13460526894d7e2e2397 to your computer and use it in GitHub Desktop.
Track velocity in list view
@Override
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 super.onTouchEvent(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment