Android 屏幕双击事件的捕获简单示例_安卓软件开发教程-查字典教程网
Android 屏幕双击事件的捕获简单示例
Android 屏幕双击事件的捕获简单示例
发布时间:2016-12-28 来源:查字典编辑
摘要:在Android游戏开发中,我们可能经常要像PC操作一样在屏幕上双击。对于屏幕双击操作,Android1.6版本以前并没有提供完善的手势识别...

在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应用的朋友。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新安卓软件开发学习
    热门安卓软件开发学习
    编程开发子分类