Cyh的博客

          Email:kissyan4916@163.com
          posts - 26, comments - 19, trackbacks - 0, articles - 220

          Android游戲開發之旅(二十) 雙按事件捕獲

          Posted on 2010-12-03 10:34 啥都寫點 閱讀(553) 評論(0)  編輯  收藏 所屬分類: Android

          對于游戲開發中我們可能經常要用到雙按屏幕,在Android 1.6以前并沒有提供完善的手勢識別類,在Android 1.5 SDK中我們可以找到android.view.GestureDetector.OnDoubleTapListener,但是經過測試仍然無法正常工作,不知道什么原因,如果您知道可以聯系android123@163.com 共享下。最終我們使用的解決方法如下

               最終我們測試的如下:

          view plaincopy to clipboardprint?
          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;  
                  }  
               }  

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

           

          本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/JavaTiger427/archive/2010/11/25/6034698.aspx



                                                                                                                 --    學海無涯
                  

          主站蜘蛛池模板: 莫力| 平顶山市| 平阴县| 岳阳市| 南投市| 武胜县| 盐边县| 汤阴县| 巫溪县| 榆社县| 商水县| 噶尔县| 紫阳县| 金华市| 永福县| 彝良县| 普安县| 屏东县| 白城市| 清徐县| 古交市| 聂拉木县| 临洮县| 凤城市| 苏尼特左旗| 长沙市| 化德县| 河北省| 巴中市| 方城县| 云和县| 石河子市| 乡城县| 铁力市| 革吉县| 扎赉特旗| 来宾市| 中西区| 嘉善县| 云浮市| 广元市|