我是Android編程的新手,所以我要求您在我的問題中提供幫助。 我正在嘗試以秒/毫秒爲單位測量MouseEvent.ACTION_DOWN和MouseEvent.ACTION_UP之間的時間量。在Android中測量兩個MotionEvents之間的運行時間
@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
我的問題在於事實秒變量不顯示我的 想,我甚至不知道它的測量correctly.For上述 例如持續時間爲16545(???!但它應該在 之間1-3秒。我該怎麼做才能在幾秒鐘內正確測量或者兩個MotionEvents之間的時間爲012毫秒,或者我的例子中 的錯誤是什麼?謝謝 !
謝謝你,看着你的代碼,我發現我的錯:可變長的啓動應該聲明的全局,在我的功能不是本地的,非常感謝你! – Matey 2012-03-19 02:51:19
您應該使用'System.nanoTime()'而不是'System.currentTimeMillis()'。請閱讀javadoc的兩個函數,據說'currentTimeMillis()'不應該用於測量時間間隔,因爲它可能會在運行時調整。 – keaukraine 2013-03-05 13:36:32
感謝keaukraine,使用納米時間()適應的變化 – 2013-08-16 07:32:26