测量机器人2 MotionEvents之间经过的时间 [英] Measure elapsed time between two MotionEvents in Android

查看:122
本文介绍了测量机器人2 MotionEvents之间经过的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid的编程,所以我要求你在我的问题的帮助。 我想以秒为单位来衡量/毫秒的MouseEvent.ACTION_DOWN和MouseEvent.ACTION_UP之间的时间量。

  @覆盖
公共布尔的onTouchEvent(MotionEvent事件){
    长时间启动= 0;
    如果(event.getAction()== MotionEvent.ACTION_DOWN){
        //管理下preSS
        启动= System.nanoTime(); //启动
        的System.out.println(START);
    }
    否则如果(event.getAction()== MotionEvent.ACTION_MOVE){
        //管理举措
        的System.out.println(event.getRawX()+,+ event.getRawY());
    }
    其他 {
        //管理多达
        悠长= System.nanoTime()// FINISH
        长秒=(完成启动)/ 10亿; //为秒
        Toast.makeText(这一点,说完,持续的时间:+秒,Toast.LENGTH_SHORT).show();
        的System.out.println(说完,持续的时间:+秒);
    }
    返回true;
}




logcat的:
4月3日至19号:04:27.140:我/的System.out(4348):开始
4月3日至19号:04:27.160:我/的System.out(4348):517.0,280.0
4月3日至19号:04:27.190:我/的System.out(4348):517.0,280.0
4月3日至19号:04:27.200:我/的System.out(4348):517.0,280.0
4月3日至19号:04:27.220:我/的System.out(4348):517.0,280.0
4月3日至19号:04:27.250:我/的System.out(4348):517.0,280.0
4月3日至19号:04:27.260:我/的System.out(4348):517.0,280.0
4月3日至19号:04:27.300:我/的System.out(4348):517.0,280.0
4月3日至19号:04:27.310:我/的System.out(4348):517.0,280.0
4月3日至19号:04:27.330:我/的System.out(4348):完成,持续时间:16545
 

  

我的问题存在于事实秒变量不显示我的   希望,我甚至不知道它的测量correctly.For以上   例如持续时间为16545(???!?!?),但它应该是的   1-3 seconds.What应当做些什么来衡量秒正确或   毫秒2 MotionEvents或者我究竟错在之间的时间   我的例子吗?谢谢!

解决方案

 长的startTime;
公共布尔的onTouchEvent(MotionEvent事件){

    如果(event.getAction()== MotionEvent.ACTION_DOWN)
        的startTime = System.nanoTime();

    否则如果(event.getAction()== MotionEvent.ACTION_UP){
        长ela​​pseTime = System.nanoTime() -  startTime时;
        //做什么ü希望与elapseTime现在,它在纳秒
    }
}
 

I'm new in Android programming so I am asking for your help in my problem. I am trying to measure in seconds/milliseconds the amount of time between a MouseEvent.ACTION_DOWN and 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 ? 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
    }
}

这篇关于测量机器人2 MotionEvents之间经过的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆