19

我需要在我的自定义视图的 ontouch 方法中进行单点触摸检测。我尝试在 ACTION-DOWN 和 ACTION-UP 和 ACTION-UP 中获取 x 和 y 值.

我的代码如下

@Override
public 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;
}

但我无法完成它。我的意思是在每次操作时都会调用我的方法。即使 actionup 和 actiondown 的 x 和 y 值不相同。而且我认为当我们用手指在屏幕上触摸时,我还需要为单击设置一些范围。谁能给我一些建议?

4

7 回答 7

42

要检测 android 中的单击和双击,我使用以下方法:

class GestureTap extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.i("onDoubleTap :", "" + e.getAction());
        return true;
   }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.i("onSingleTap :", "" + e.getAction());
        return true;
    }
}

在 GestureDetector 的构造函数中使用它:

detector = new GestureDetector(this, new GestureTap());

并在 onTouch 监听器中添加以下代码

@Override
public boolean onTouchEvent(MotionEvent event) {

    detector.onTouchEvent(event);

    return true;
}
于 2014-05-17T17:11:59.637 回答
15

为了完整起见添加答案,如果其他人到达这里:

您可以将 aGestureDetector与 an 一起使用OnTouchListener

final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
         //do something
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return super.onDoubleTap(e);
        }
    });

 viewToTouch.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return gestureDetector.onTouchEvent(event);
        }
    });
于 2015-10-27T14:11:34.393 回答
9

我最近也遇到了同样的问题,最终不得不实施去抖来让它工作。这并不理想,但在我找到更好的东西之前它非常可靠。

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;
        }
    };
}
于 2013-08-01T07:12:51.140 回答
4

有一种更简单直接的方法。使用 MotionEvent.ACTION_DOWN && MotionEvent.ACTION_UP 并计时事件之间的差异。

完整的代码可以在这里找到。https://stackoverflow.com/a/15799372/3659481

setOnTouchListener(new OnTouchListener() {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            startClickTime = Calendar.getInstance().getTimeInMillis();
            break;
        }
        case MotionEvent.ACTION_UP: {
            long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
            if(clickDuration < MAX_CLICK_DURATION) {
                //click event has occurred
            }
        }
    }
    return true;
}

}

于 2014-07-31T17:56:54.723 回答
4

为视图添加GestureDetector.SimpleOnGestureListener并在此使用方法onSingleTapConfirmed

仅当 Android 操作系统确认特定的触摸是单击而不是双击时,才会调用此方法。

你可以谷歌搜索安卓示例。

于 2013-08-01T07:54:53.943 回答
0

认为您不需要使用“等于”运算符。而不是使用近似值

case MotionEvent.ACTION_DOWN: {
    final int CONST = 5;
    final float x = ev.getX();
    final float y = ev.getY();

    mLastTouchXMax = x+CONST;  //here i get x and y values in action down
    mLastTouchXMin = x-CONST;
    mLastTouchYMax = y+CONST;
    mLastTouchYMin = y-CONST;
    mActivePointerId = ev.getPointerId(0);

    break;
}

并在 ACTION_UP 检查区间之间的 X 和 Y 值。

于 2013-08-01T06:33:53.577 回答
0
float dX,dY,x,y;     
tv.setOnTouchListener(new View.OnTouchListener() {
                                @Override
                                public boolean onTouch(View view, MotionEvent event) {
                                    switch (event.getAction()) {
                                        case MotionEvent.ACTION_UP:     //Event for On Click
                                            if(x==view.getX() && y==view.getY()){
                                                Toast.makeText(getApplicationContext(),"TextView Clicked",Toast.LENGTH_LONG).show();
                                            }
                                            break;
                                        case MotionEvent.ACTION_DOWN:
                                            x=view.getX();
                                            y=view.getY();
                                            dX = view.getX() - event.getRawX();
                                            dY = view.getY() - event.getRawY();
                                            break;

                                        case MotionEvent.ACTION_MOVE:
                                            view.animate()
                                                    .x(event.getRawX() + dX)
                                                    .y(event.getRawY() + dY)
                                                    .setDuration(0)
                                                    .start();
                                            break;
                                        default:
                                            return false;
                                    }
                                    return true;
                                }
                            });
于 2017-01-31T15:20:15.143 回答