gpt4 book ai didi

java - 测量 Android 中两个 MotionEvents 之间耗时

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:08 24 4
gpt4 key购买 nike

我是 Android 编程的新手,所以我请求您帮助解决我的问题。我正在尝试以秒/毫秒为单位测量 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 ? Thank you !

最佳答案

MotionEvent 有一个时间戳。使用 getEventTime() 访问它。

事实上,由于无法保证 MotionEvent 会立即传送到您的代码,因此此时间戳比您从 System.getCurrentTimeMillis() 获得的任何时间都更准确>.

关于java - 测量 Android 中两个 MotionEvents 之间耗时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com