Is it possible to get the speed or velocity or acceleration of touch event in android with the existing api? I have gone through MotionEvent class and none of the fields in that class seem to retrieve information that i need. Any help would be greatly appreciated
Asked
Active
Viewed 7,262 times
4
-
MotionEvents don't track accelerometer events: see here: http://developer.android.com/reference/android/view/MotionEvent.html You need to get an instance of SensorManager for accelerometer events There's a great example here: http://www.techrepublic.com/blog/app-builder/a-quick-tutorial-on-coding-androids-accelerometer/472 – Jian Chen Apr 14 '12 at 19:52
-
I think he don't mean accelerometer. – Dmitry Zaytsev Apr 14 '12 at 19:59
-
Yes, i dont mean accelerometer. What i meant was speed or velocity of gesture – user1253887 Apr 14 '12 at 21:03
2 Answers
14
MotionEvent does not help you in this case. You can use VelocityTracker class. It gets MotionEvent instances and calculates velocity of recent touch events. You can take a look at its documentation here: http://developer.android.com/reference/android/view/VelocityTracker.html
First you have to get an instance by obtain() method:
VelocityTracker velocity = VelocityTracker.obtain();
then you can add ACTION_MOVE events to its queue:
if(event.getAction() == MotionEvent.ACTION_MOVE)
{
velocity.addMovement(event);
}
then you can compute velocity and extract x_velocity and y_velocity
velocity.computeCurrentVelocity(1000);
float x_velocity = velocity.getXVelocity();
float y_velocity = velocity.getYVelocity();
I hope it works for you.

Ali Samz
- 161
- 1
- 5
0
You need to calculate velocity manually between each two events using System.nanoTime
. Same way for acceleration (but using velocity instead of coordinates this time).

Dmitry Zaytsev
- 23,650
- 14
- 92
- 146