Android游戏开发之十九:屏幕双击事件的捕获

移动开发 Android 游戏开发
关于android双击事件 我相信大家都知道API中是有个方法的,但是必须在Activity中在能使用。对于到底用不用android双击事件API各有各的看法。在Activity中使用API,省时省力,别人写的东西,直接用就行了,不担心有BUG,但是代码写在activity中总感觉有些乱;自己写个onDoubleClick方法,自己写的东西,好控制,灵活性强,想放哪放哪,但是需要消耗点时间,检查BUG之类的。

在Android游戏开发中,我们可能经常要像PC操作一样在屏幕上双击。对于屏幕双击操作,Android 1.6版本以前并没有提供完善的手势识别类,Android 1.5的SDK中提供了android.view.GestureDetector.OnDoubleTapListener,但经测试无法正常工作,不知是何原因。最终我们的解决方案如下面的代码:

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;    
        }    
     }    
}   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

 

责任编辑:闫佳明 来源: jizhuomi
相关推荐

2013-09-13 13:15:28

AndroidWebViewJavaScript

2013-05-21 14:15:23

Android游戏开发屏幕分辨率

2013-05-21 11:33:11

Android游戏开发按键中断事件

2013-05-20 16:53:55

Android游戏开发长按事件

2013-04-15 15:22:06

2013-05-21 10:42:48

Android游戏开发Bitmap位图旋转

2013-07-29 04:29:29

iOS开发iOS开发学习禁用UITabBarC

2013-05-21 11:26:49

Android游戏开发Sensor感应

2013-05-20 17:51:47

Android游戏开发SurfaceView

2009-09-10 18:18:42

C# Button

2015-02-03 14:45:55

android全局异常

2013-05-20 17:13:17

Android游戏开发CanvasPaint

2013-05-20 17:48:20

2013-05-21 09:56:15

2013-05-21 13:55:51

Android游戏开发图像渐变特效

2013-05-21 11:24:07

Android游戏开发Sensor重力感应

2016-10-20 19:07:10

Javascript事件冒泡与捕获

2011-06-02 10:09:18

2013-05-20 15:42:22

2013-05-20 17:33:44

Android游戏开发自定义View
点赞
收藏

51CTO技术栈公众号