掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
在Android游戲開發(fā)中,我們可能經(jīng)常要像PC操作一樣在屏幕上雙擊。對于屏幕雙擊操作,Android 1.6版本以前并沒有提供完善的手勢識別類,Android 1.5的SDK中提供了android.view.GestureDetector.OnDoubleTapListener,但經(jīng)測試無法正常工作,不知是何原因。最終我們的解決方案如下面的代碼:

- public class TouchLayout extends RelativeLayout {
- public Handler doubleTapHandler = null;
- protected long lastDown = -1;
- public final static long DOUBLE_TIME = 500;
- public TouchLayout(Context context) {
- super(context);
- }
- public TouchLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public TouchLayout(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public boolean onTouchEvent(MotionEvent event) {
- this.handleEvent(event);
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- long nowDown = System.currentTimeMillis();
- if (nowDown - lastDown < DOUBLE_TIME)
- {
- if (doubleTapHandler != null)
- doubleTapHandler.sendEmptyMessage(-1);
- } else {
- lastDown = nowDown;
- }
- }
- return true;
- }
- protected void handleEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- //Do sth 這里處理即可
- break;
- case MotionEvent.ACTION_UP:
- //Do sth
- break;
- }
- }
- }

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流