Measure elapsed time between two MotionEvents in Android
long startTime;
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
startTime = System.nanoTime();
else if (event.getAction() == MotionEvent.ACTION_UP) {
long elapseTime = System.nanoTime() - startTime;
//do whatever u want with elapseTime now, its in nanoseconds
}
}
A MotionEvent
has a timestamp. Use getEventTime()
to access it.
In fact, since there is no guarantee that the MotionEvent
is delivered immediately to your code, this timestamp is more accurate than any times you get from System.getCurrentTimeMillis()
.