通过onTouchEvent判断是否为双击事件,通过前面一次up 跟本次down之间的时间差,并且点击的点的坐标偏移来确定是否为双击事件DOUBLE_TAP_TIMEOUT。
boolean isMove = false; private final int DOUBLE_TAP_TIMEOUT = 200; private MotionEvent mCurrentDownEvent; private boolean mDoubleTapEnabled = false; private MotionEvent mPreviousUpEvent; @Override public void onTouchEvent(MotionEvent event) { Log.e(LOG_TAG, "MotionEvent="+ event.getAction()+",isMove="+isMove); Log.e(LOG_TAG, "mPreviousUpEvent="+ mPreviousUpEvent+",mCurrentDownEvent="+mCurrentDownEvent); if (event.getAction() == MotionEvent.ACTION_MOVE) { isMove = true; } else if (event.getAction() == MotionEvent.ACTION_DOWN) { if (mPreviousUpEvent != null && mCurrentDownEvent != null && isConsideredDoubleTap(mCurrentDownEvent, mPreviousUpEvent, event) ) { if (mDoubleTapEnabled && Environment.getExternalStorageState().equals("mounted")) { Toast.makeText(getApplicationContext(), getResources().getString(R.string.double_click), Toast.LENGTH_LONG).show(); } } mCurrentDownEvent = MotionEvent.obtain(event); }else if (event.getAction() == MotionEvent.ACTION_UP){ mPreviousUpEvent = MotionEvent.obtain(event); isMove = false; } } 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; }