Mat*_*tey 7 java time android elapsed
我是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
Run Code Online (Sandbox Code Playgroud)
我的问题实际上是秒变量没有显示我想要的东西,我甚至不知道它的测量是否正确.对于上面的例子,持续时间是16545(???!?!?)但是它应该在1之间-3秒.我应该怎么做才能在几秒或几毫秒内正确测量两个MotionEvent之间的时间或我的例子中的错误?谢谢 !
Rah*_*arg 11
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
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
A MotionEvent有时间戳.使用getEventTime()访问它.
事实上,由于不能保证MotionEvent被立即传送到你的代码,这个时间戳比你得到任何时候更加准确System.getCurrentTimeMillis().
| 归档时间: |
|
| 查看次数: |
9430 次 |
| 最近记录: |