android – 单眼触摸检测在Ontouch方法的视图

android – 单眼触摸检测在Ontouch方法的视图,第1张

概述我需要在自定义视图的onuch方法中进行单边触摸检测.我尝试在ACTION-DOWN和ACTION-UP中获取x和y值,而在ACTION-UP中我给出的条件是,如果ACTIONDOWN和ACTION-UP中的X和Y的值相等,则将其作为单次点击. 我的代码如下 @Overridepublic boolean onTouchEvent(MotionEvent ev) { if (!mSuppo 我需要在自定义视图的onuch方法中进行单边触摸检测.我尝试在ACTION-DOWN和ACTION-UP中获取x和y值,而在ACTION-UP中我给出的条件是,如果ACTIONDOWN和ACTION-UP中的X和Y的值相等,则将其作为单次点击.

我的代码如下

@OverrIDepublic boolean ontouchEvent(MotionEvent ev) {   if (!mSupportsZoom && !mSupportsPan) return false;    mScaleDetector.ontouchEvent(ev);    final int action = ev.getAction();    switch (action & MotionEvent.ACTION_MASK) {    case MotionEvent.ACTION_DOWN: {        final float x = ev.getX();        final float y = ev.getY();        mLasttouchX = x;  //here i get x and y values in action down        mLasttouchY = y;        mActivePointerID = ev.getPointerID(0);        break;    }    case MotionEvent.ACTION_MOVE: {        final int pointerIndex = ev.findPointerIndex(mActivePointerID);        final float x = ev.getX(pointerIndex);        final float y = ev.getY(pointerIndex);        if (mSupportsPan && !mScaleDetector.isInProgress()) {            final float dx = x - mLasttouchX;            final float dy = y - mLasttouchY;            mPosX += dx;            mPosY += dy;            //mFocusX = mPosX;            //mFocusY = mPosY;            invalIDate();        }        mLasttouchX = x;        mLasttouchY = y;        break;    }    case MotionEvent.ACTION_UP: {        final float x = ev.getX();        final float y = ev.getY();        touchupX=x;   //here is get x and y values at action up        touchupY=y;         if(mLasttouchX == touchupX && mLasttouchY == touchupY){  //my condition if both the x and y values are same .            PinchZoomPanActivity2.tapped1(this.getContext(),100); //my method if the singletap is detected        }        else{        }        mActivePointerID = INVALID_POINTER_ID;        break;    }    case MotionEvent.ACTION_CANCEL: {        mActivePointerID = INVALID_POINTER_ID;        break;    }    case MotionEvent.ACTION_POINTER_UP: {        final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)                 >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;        final int pointerID = ev.getPointerID(pointerIndex);        if (pointerID == mActivePointerID) {            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;            mLasttouchX = ev.getX(newPointerIndex);            mLasttouchY = ev.getY(newPointerIndex);            mActivePointerID = ev.getPointerID(newPointerIndex);        }        break;    }    }    return true;}

但我不能完成.我的意思是在每一个动作,我的方法被称为.即使动作和动作的x和y值都不相同.我想我也需要把一些范围放在单边,我们用手指触摸屏幕.有人可以给我一些建议吗

解决方法 最近我也遇到了同样的问题,最终不得不实施去抖动来使其工作.这不是理想的,但它是非常可靠的,直到我能找到更好的东西.

View.onClickListener对我来说更可靠,但不幸的是,我需要OntouchListener的MotionEvent.

编辑:删除导致其在此失败的多余代码

class CustomVIEw extends VIEw {    private static long mDeBounce = 0;    static OntouchListener ListenerMotionEvent = new OntouchListener() {        @OverrIDe        public boolean ontouch(VIEw vIEw,MotionEvent motionEvent) {            if ( Math.abs(mDeBounce - motionEvent.getEventTime()) < 250) {                //Ignore if it's been less then 250ms since                //the item was last clicked                return true;            }            int intCurrentY = Math.round(motionEvent.getY());            int intCurrentX = Math.round(motionEvent.getX());            int intStartY = motionEvent.getHistorySize() > 0 ? Math.round(motionEvent.getHistoricalY(0)) : intCurrentY;            int intStartX = motionEvent.getHistorySize() > 0 ? Math.round(motionEvent.getHistoricalX(0)) : intCurrentX;            if ( (motionEvent.getAction() == MotionEvent.ACTION_UP) && (Math.abs(intCurrentX - intStartX) < 3) && (Math.abs(intCurrentY - intStartY) < 3) ) {                if ( mDeBounce > motionEvent.getDownTime() ) {                    //Still got occasional duplicates without this                    return true;                }                //Handle the click                mDeBounce = motionEvent.getEventTime();                return true;            }            return false;        }    };}
总结

以上是内存溢出为你收集整理的android – 单眼触摸检测在Ontouch方法的视图全部内容,希望文章能够帮你解决android – 单眼触摸检测在Ontouch方法的视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1132702.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存