2012-03-19 10 views
7

Soy nuevo en la programación de Android, así que le pido ayuda en mi problema. Estoy tratando de medir en segundos/milisegundos la cantidad de tiempo entre MouseEvent.ACTION_DOWN y MouseEvent.ACTION_UP.Mide el tiempo transcurrido entre dos MotionEvents en Android

@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 

Mi problema consiste en que la variable segundos no muestra lo que yo quiero , que incluso no sé si su medición correctly.For la ejemplo, la duración anteriormente era 16545 (???! ?!?) pero debería haber sido entre 1-3 segundos. ¿Qué debo hacer para medir correctamente en segundos o milisegundos el tiempo entre dos MotionEvents o qué estoy mal en mi ejemplo? Gracias !

Respuesta

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 
    } 
} 
+0

Gracias, mirando a su código descubrí mi culpa: inicio de largo variable debe ser declarada global, no local en mi función, muchas gracias! – Matey

+3

Debe usar 'System.nanoTime()' en lugar de 'System.currentTimeMillis()'. Por favor, lea javadoc para ambas funciones, se dice que 'currentTimeMillis()' no debe usarse para medir intervalos porque puede ajustarse en tiempo de ejecución. – keaukraine

+0

Gracias keaukraine, cambios acomodados para usar nanoTime() –

8

Un MotionEvent tiene una marca de tiempo. Use getEventTime() para acceder a él.

De hecho, como no hay garantía de que el MotionEvent se entregue inmediatamente a su código, esta marca de tiempo es más precisa que cualquier otra vez que obtenga de System.getCurrentTimeMillis().

+1

+1 Esto, usado con 'android.os.SystemClock.uptimeMillis()' es en realidad la solución correcta para encontrar el tiempo entre eventos de bajada y subida. –

0

Aquí está la solución descrita por @CvR:

private long startTimeInMilliSec; 
@Override 
public boolean onTouchEvent(MotionEvent event) { 

    if (event.getAction() == MotionEvent.ACTION_DOWN) 
     startTimeInMilliSec = event.getEventTime();  

    else if (event.getAction() == MotionEvent.ACTION_UP) { 
     long elapsedTime = event.getEventTime() - startTimeInMilliSec; 
     //do whatever u want with elapsedTime now, its in milliseconds 
    } 
} 
Cuestiones relacionadas