简体   繁体   English

测量Android中两个MotionEvent之间的经过时间

[英]Measure elapsed time between two MotionEvents in Android

I'm new in Android programming so I am asking for your help in my problem. 我是Android编程的新手,所以我在问题上请求你的帮助。 I am trying to measure in seconds/milliseconds the amount of time between a MouseEvent.ACTION_DOWN and MouseEvent.ACTION_UP. 我试图以秒/毫秒为单位测量MouseEvent.ACTION_DOWN和MouseEvent.ACTION_UP之间的时间。

@Override
public boolean onTouchEvent(MotionEvent event) {
    long start=0;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // manage down press
        start=System.nanoTime();//START
        System.out.println("START");
    }
    else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        // manage move
        System.out.println(event.getRawX()+","+event.getRawY());
    }
    else {
        // manage up
        long finish=System.nanoTime()//FINISH
        long seconds = (finish-start) / 1000000000;//for seconds
        Toast.makeText(this, "FINISH, duration: "+seconds, Toast.LENGTH_SHORT).show();
        System.out.println("FINISH, duration: "+seconds);
    }
    return true;
}




Logcat:
03-19 04:04:27.140: I/System.out(4348): START
03-19 04:04:27.160: I/System.out(4348): 517.0,280.0
03-19 04:04:27.190: I/System.out(4348): 517.0,280.0
03-19 04:04:27.200: I/System.out(4348): 517.0,280.0
03-19 04:04:27.220: I/System.out(4348): 517.0,280.0
03-19 04:04:27.250: I/System.out(4348): 517.0,280.0
03-19 04:04:27.260: I/System.out(4348): 517.0,280.0
03-19 04:04:27.300: I/System.out(4348): 517.0,280.0
03-19 04:04:27.310: I/System.out(4348): 517.0,280.0
03-19 04:04:27.330: I/System.out(4348): FINISH, duration: 16545

My problem consist in fact that seconds variable doesn't show what I want, I even don't know if its measuring correctly.For the above example duration was 16545 (???!?!?) but it should have been between 1-3 seconds.What shall I do to measure correctly in seconds or milliseconds the time between two MotionEvents or what am I wrong in my example ? 我的问题实际上是秒变量没有显示我想要的东西,我甚至不知道它的测量是否正确。对于上面的例子,持续时间是16545(???!?!?)但是它应该在1之间-3秒。我应该怎么做才能在几秒或几毫秒内正确测量两个MotionEvent之间的时间或我的例子中的错误? Thank you ! 谢谢 !

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. MotionEvent有一个时间戳。 Use getEventTime() to access it. 使用getEventTime()来访问它。

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() . 实际上,由于无法保证MotionEvent立即传递给您的代码,因此该时间戳比从System.getCurrentTimeMillis()获得的任何时间都更准确。

Here is the solution described by @CvR: 以下是@CvR描述的解决方案:

private long startTimeInMilliSec;
@Override 
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) 
        startTimeInMilliSec = event.getEventTime();    

    else if (event.getAction() == MotionEvent.ACTION_UP) {
        long elapsedTime = event.getEventTime() - startTimeInMilliSec;
        //do whatever u want with elapsedTime now, its in milliseconds 
    } 
} 

Each event also have getDownTime() method. 每个事件也都有getDownTime()方法。 So when you received MotionEvent.ACTION_UP you can simple use 因此,当您收到MotionEvent.ACTION_UP您可以轻松使用

event.getDownTime() - event.getEventTime()

to calculate elapsed time. 计算经过的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM