Created
October 25, 2014 16:43
-
-
Save smile0520/ff05f5ecd019176b86ce to your computer and use it in GitHub Desktop.
連続N回タップを判定するクラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.SystemClock; | |
import java.util.Arrays; | |
/** | |
* 連続N回タップを判定するクラス | |
*/ | |
public class HitDetector { | |
private final long[] mHits; | |
private final long mThresholdTimeMillis; | |
/** | |
* コンストラクタ | |
* | |
* @param hitCount ヒット判定回数 | |
* @param thresholdTimeMillis hit閾値時間(ms) | |
*/ | |
public HitDetector(int hitCount, long thresholdTimeMillis) { | |
if (hitCount <= 0 || thresholdTimeMillis <= 0) { | |
throw new IllegalArgumentException( | |
"Arguments must be hitCount > 0 and thresholdTimeMillis > 0."); | |
} | |
mHits = new long[hitCount]; | |
mThresholdTimeMillis = thresholdTimeMillis; | |
} | |
/** | |
* 連続ヒット判定を行います | |
* | |
* @return 連続ヒットされた場合true,それ以外false | |
*/ | |
public boolean hit() { | |
System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1); | |
long uptimeMillis = SystemClock.uptimeMillis(); | |
mHits[mHits.length - 1] = uptimeMillis; | |
boolean detect = mHits[0] >= (uptimeMillis - mThresholdTimeMillis); | |
// 連続ヒットした場合は今までのヒットをクリアする | |
if(detect){ | |
Arrays.fill(mHits, 0); | |
} | |
return detect; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment