speed or acceleration of motion event android

老子叫甜甜 提交于 2019-12-10 17:32:22

问题


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


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/10155907/speed-or-acceleration-of-motion-event-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!