我是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
我的问题实际上是秒变量不显示我想要的内容,我甚至不知道它是否正确地进行测量。对于上述示例,持续时间为16545(???!?!?),但应该在1-3秒之间。我应该怎么做才能正确测量两个MotionEvents之间的时间,或者我在示例中错了吗?谢谢!
android.os.SystemClock.uptimeMillis()
结合使用,实际上是找到按下和释放事件之间时间的正确解决方案。 - Nick Hartung