使用Google提供的api中采用的算法
能够实现n次点击事件,我们需要定义一个n长度的数组,每点击一次将数组里的内容按序号整体向左移动一格,然后给n-1出即数组的最后添加当前的时间,如果0个位置的时间大于当前时间减去1000毫秒的话,那么证明在1000毫秒内点击了n次。实现如下:
private final static int COUNTS = 3;
private final static int DURATION = 1000;
private long[] mHits = new long[COUNTS];
private void countinuousClick(int count, long time) {
System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
mHits[mHits.length - 1] = SystemClock.uptimeMillis();
if (DURATION >= (SystemClock.uptimeMillis() - mHits[0])) {
mHits = new long[COUNTS];
clearLog();
}
}
private void initListeners() {
mLogInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
countinuousClick(COUNTS, DURATION);
}
});
}