android中根據touch事件判斷單擊及雙擊

來源:互聯網
上載者:User

標籤:android   java   os   io   for   ar   cti   amp   

private static final int MAX_INTERVAL_FOR_CLICK = 250;    private static final int MAX_DISTANCE_FOR_CLICK = 100;    private static final int MAX_DOUBLE_CLICK_INTERVAL = 500;    int mDownX = 0;    int mDownY = 0;    int mTempX = 0;    int mTempY = 0;    boolean mIsWaitUpEvent = false;    boolean mIsWaitDoubleClick = false;    Runnable mTimerForUpEvent = new Runnable() {        public void run() {            if (mIsWaitUpEvent) {                Log.d(LOG_TAG,                        "The mTimerForUpEvent has executed, so set the mIsWaitUpEvent as false");                mIsWaitUpEvent = false;            } else {                Log.d(LOG_TAG,                        "The mTimerForUpEvent has executed, mIsWaitUpEvent is false,so do nothing");            }        }    };    @Override    public boolean onTouchEvent(MotionEvent event) {        if (!mIsWaitUpEvent && event.getAction() != MotionEvent.ACTION_DOWN) {            return super.onTouchEvent(event);        }        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:            mDownX = (int) event.getX();            mDownY = (int) event.getY();            mIsWaitUpEvent = true;            postDelayed(mTimerForUpEvent, MAX_INTERVAL_FOR_CLICK);            break;        case MotionEvent.ACTION_MOVE:            mTempX = (int) event.getX();            mTempY = (int) event.getY();            if (Math.abs(mTempX - mDownX) > MAX_DISTANCE_FOR_CLICK                    || Math.abs(mTempY - mDownY) > MAX_DISTANCE_FOR_CLICK) {                mIsWaitUpEvent = false;                removeCallbacks(mTimerForUpEvent);                Log.d(LOG_TAG, "The move distance too far:cancel the click");            }            break;        case MotionEvent.ACTION_UP:            mTempX = (int) event.getX();            mTempY = (int) event.getY();            if (Math.abs(mTempX - mDownX) > MAX_DISTANCE_FOR_CLICK                    || Math.abs(mTempY - mDownY) > MAX_DISTANCE_FOR_CLICK) {                mIsWaitUpEvent = false;                removeCallbacks(mTimerForUpEvent);                Log.d(LOG_TAG,                        "The touch down and up distance too far:cancel the click");                break;            } else {                mIsWaitUpEvent = false;                removeCallbacks(mTimerForUpEvent);                onSingleClick();                return super.onTouchEvent(event);            }        case MotionEvent.ACTION_CANCEL:            mIsWaitUpEvent = false;            removeCallbacks(mTimerForUpEvent);            Log.d(LOG_TAG, "The touch cancel state:cancel the click");            break;        default:            Log.d(LOG_TAG, "irrelevant MotionEvent state:" + event.getAction());        }        return super.onTouchEvent(event);    }    Runnable mTimerForSecondClick = new Runnable() {        @Override        public void run() {            if (mIsWaitDoubleClick) {                Log.d(LOG_TAG,                        "The mTimerForSecondClick has executed,so as a singleClick");                mIsWaitDoubleClick = false;                // at here can do something for singleClick!!            } else {                Log.d(LOG_TAG,                        "The mTimerForSecondClick has executed, the doubleclick has executed ,so do thing");            }        }    };    public void onSingleClick() {        if (mIsWaitDoubleClick) {            onDoubleClick();            mIsWaitDoubleClick = false;            removeCallbacks(mTimerForSecondClick);        } else {            mIsWaitDoubleClick = true;            postDelayed(mTimerForSecondClick, MAX_DOUBLE_CLICK_INTERVAL);        }    }    public void onDoubleClick() {        Log.d(LOG_TAG,"we can do sth for double click here");    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.