在Android中,如何通过onTouchEvent实现Double Click,判断为双击事件

来源:互联网 发布:大理石 氡气 知乎 编辑:程序博客网 时间:2024/06/05 12:38

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