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

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() {

		@Override
		public 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,android,双击,OnTouchListener,java)