If you think the Android project Airplanes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.axnsan.airplanes.util;
//fromwww.java2s.compublicclass TapDetector implements Detector {
/**Maximum slop(distance between down location and up location), in pixels**/publicstaticfinalint SLOP = 20;
/**Max delay between touch down and touch up, in seconds**/publicstaticfinalfloat DELAY = 0.2f;
privatefloat delta = 0.f;
privateint x, y;
privateboolean waitingForUp = false;
private TapListener listener = null;
public TapDetector(TapListener lis) {
listener = lis;
}
publicvoid down(int x, int y) {
waitingForUp = true;
this.x = x; this.y = y;
/* We set the initial duration to -1, and only set it to 0 and begin counting in the next frame.
* This necessary to ensure we don't count the time before this frame.
* We must do this because rendering only happens when waiting for input,
* and as such frame delta can be very high if the last input was long ago.*/
delta = -1.f;
}
publicvoid up(int x, int y) {
if (waitingForUp) {
if (delta <= DELAY && Math.abs(this.x - x) < SLOP && Math.abs(this.y - y) < SLOP) {
listener.tap(this.x, this.y);
}
waitingForUp = false;
}
}
publicboolean waiting() {
return waitingForUp;
}
publicvoid elapsed(float delta) {
if (this.delta < 0.f) /*This step is necessary to ensure we don't count the time before the first tap*/
this.delta = 0.f;
else {
this.delta += delta;
if (this.delta > DELAY) /*If we passed the max delay, stop waiting*/
waitingForUp = false;
}
}
}