Created
July 22, 2015 01:33
-
-
Save Viyu/d06eda19f9bcf7223f6b to your computer and use it in GitHub Desktop.
Multi Touch Listener
This file contains 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
/** | |
* Multi touch event listener | |
* | |
* @author viyu | |
* | |
*/ | |
public abstract class OnMultiTouchListener implements OnTouchListener { | |
/** | |
* last touch time | |
*/ | |
private long lastTouchTime = 0; | |
/** | |
* touched count | |
*/ | |
private AtomicInteger touchCount = new AtomicInteger(0); | |
private Runnable mRun = null; | |
private Handler mHandler = new Handler(); | |
public void removeCallback() { | |
mHandler.removeCallbacks(mRun); | |
} | |
@Override | |
public boolean onTouch(final View v, final MotionEvent event) { | |
if (event.getAction() == MotionEvent.ACTION_DOWN) { | |
final long now = System.currentTimeMillis(); | |
lastTouchTime = now; | |
touchCount.incrementAndGet(); | |
if (mRun != null) { | |
mHandler.removeCallbacks(mRun); | |
} | |
mRun = new Runnable() { | |
@Override | |
public void run() { | |
if (now == lastTouchTime) { | |
onMultiTouch(v, event, touchCount.get()); | |
touchCount.set(0); | |
} | |
} | |
}; | |
mHandler.postDelayed(mRun, getMultiTouchInterval()); | |
} | |
return true; | |
} | |
/** | |
* max interval, default is 400, it can be overrided by subclass | |
* | |
* @return | |
*/ | |
protected int getMultiTouchInterval() { | |
return 400; | |
} | |
/** | |
* multi touch callback | |
* | |
* @param v | |
* @param event | |
* @param touchCount | |
* touch count | |
* @return | |
*/ | |
public abstract void onMultiTouch(View v, MotionEvent event, int touchCount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment