Android fully distinguishes double-tap from singal-tap.

Source: Internet
Author: User

Requirement: When viewpager is used to display an image, the image needs to be scaled when you double-click it. popwindow is displayed at the bottom of the screen when you click it. Because android double-click is essentially a two-click operation, however, I don't want to trigger the clicking action when I double-click it. So I searched for solutions on the Internet. However, after painstaking consideration, I found the following methods:

1. Override the dispatchTouchEvent method of the activity.

 1 @Override 2     public boolean dispatchTouchEvent(MotionEvent event) { 3  4         if (event.getAction() == MotionEvent.ACTION_DOWN) { 5             mDownX = (int) event.getX(); 6             if (mFirstTouchEventTime == 0) { 7                 mFirstTouchEventTime = event.getDownTime(); 8             } else { 9                 mLastTouchEventTime = event.getDownTime();10             }11         } else if (event.getAction() == MotionEvent.ACTION_UP) {12             mUpX = (int) event.getX();13             if (Math.abs(mUpX - mDownX) < 30) {14                 Log.d(TAG, "--->>" + Math.abs(mFirstTouchEventTime - mLastTouchEventTime));15                 if (Math.abs(mFirstTouchEventTime - mLastTouchEventTime) < Constants.TIME_MENU_DISPLAY) {16                     mFirstTouchEventTime = 0L;17                     mLastTouchEventTime = 0L;18                     mScreenObsever.removeMessages(Constants.MSG_MENU_DISPLAY);19                 } else {20                     mScreenObsever.sendEmptyMessageDelayed(Constants.MSG_MENU_DISPLAY, Constants.TIME_MENU_DISPLAY);21                 }22             } else {23                 mFirstTouchEventTime = 0L;24                 mLastTouchEventTime = 0L;25             }26         }27 28         return super.dispatchTouchEvent(event);29     }
MFirstTouchEventTime and mLastTouchEventTime are two long variables used to record the time when two ACTION_DOWN actions occur. Constants. TIME_MENU_DISPLAY is a custom msg what. MScreenObserver is a custom handler used to process click events.
2. handler class implementation of mScreenObserver
private class ObseverOperateHandler extends Handler {@Overridepublic void handleMessage(Message msg) {if (msg.what == Constants.MSG_MENU_DISPLAY) {mFirstTouchEventTime = 0L;mLastTouchEventTime = 0L;if (mOperateMenuPopupWindow.isShowing()) {mNotePopWindow.startDownAnimation();mOperateMenuPopupWindow.dismiss();} else {mNotePopWindow.startUpAnimation();mOperateMenuPopupWindow.showAtLocation(findViewById(R.id.photoLayout), Gravity.BOTTOM, 0, 0);}}super.handleMessage(msg);}}

The part in if is to be processed in the click.

3. As for the double-click Task, You can implement GestureDetector. OnDoubleTapListener normally. You can find out how to get it online.

In addition, the Constants. TIME_MENU_DISPLAY Time Value in the Code is verified as MS in various ways, that is, the two-click interval MS is considered as a double-click.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.