Cyh的博客

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

Android游戏开发之旅(二十) 双按事件捕获

Posted on 2010-12-03 10:34 啥都写点 阅读(538) 评论(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



                                                                                                       --    学海无涯