Android 螢幕雙擊事件的捕獲簡單樣本_Android

來源:互聯網
上載者:User

在Android遊戲開發中,我們可能經常要像PC操作一樣在螢幕上雙擊。對於螢幕雙擊操作,Android 1.6版本以前並沒有提供完善的手勢識別類,Android 1.5的SDK中提供了android.view.GestureDetector.OnDoubleTapListener,但經測試無法正常工作,不知是何原因。最終我們的解決方案如下面的代碼:

Java代碼

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;      }      }    } 

以上就是對Android 螢幕雙擊的事件捕獲的範例程式碼,後續繼續補充相關資料,希望能協助開發Android應用的朋友。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.