在Android中,如何通过onTouchEvent实现Double Click,判断为双击事件
16lz
2021-12-04
How to catch double tap events in Android using OnTouchListener? double click event in android
如何通过onTouchEvent实现Double Click,判断为双击事件
private final int DOUBLE_TAP_TIMEOUT = 200;private MotionEvent mCurrentDownEvent;private MotionEvent mPreviousUpEvent;OnTouchListener mTouchListener = new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN) {if (mPreviousUpEvent != null&& mCurrentDownEvent != null&& isConsideredDoubleTap(mCurrentDownEvent,mPreviousUpEvent, event)) {Log.e(TAG, "Double click=============");}mCurrentDownEvent = MotionEvent.obtain(event);} else if (event.getAction() == MotionEvent.ACTION_UP) {mPreviousUpEvent = MotionEvent.obtain(event);}return true;}};private boolean isConsideredDoubleTap(MotionEvent firstDown,MotionEvent firstUp, MotionEvent secondDown) {if (secondDown.getEventTime() - firstUp.getEventTime() > DOUBLE_TAP_TIMEOUT) {return false;}int deltaX = (int) firstUp.getX() - (int) secondDown.getX();int deltaY = (int) firstUp.getY() - (int) secondDown.getY();return deltaX * deltaX + deltaY * deltaY < 10000;}
再通过View设置Touch监听
view.setOnTouchListener(mTouchListener);
更多相关文章
- android TextView的跑马灯效果的实现
- Android的权限permission
- Android(安卓)wifi提示已连接但是无法访问网络
- view measure详解
- android shape 常用到属性的设置
- android在java代码中绘制矩形框
- android 自定义对话框 背景透明
- Android获取通话状态
- Android(安卓)判断是否能真正上网