如何使双击和平移工作在同一时间,在Android



所以我试图让我的视图工作与许多不同的方式,应该有:

  • 缩放的多点触控
  • Touchlistener for平移
  • 双击恢复缩放
  • 或者如果它已经恢复,它将缩放双击的地方。

目前,其他一切都在工作,但当我双击并保持第二次点击并继续移动它的代码是这样工作的,它首先是restores/zooms,然后是panning starts

因此,由于缩放,它有时会先改变画布的大小,然后开始平移。我应该怎么做才能在注意到双击后启用平移?或者检查双击的最后一次点击是否保持移动,然后跳过缩放/还原功能?我的代码看起来像这样。我试图返回false后双击,但它并没有真正的影响…会有像设置ignoreMultiTouchtrue,但双击或平移?

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if(mgestureDetector.onTouchEvent(ev)){
        mOnTouchEventWorkingArray[0] = ev.getX();
        mOnTouchEventWorkingArray[1] = ev.getY();
        mOnTouchEventWorkingArray = scaledPointsToScreenPoints(mOnTouchEventWorkingArray);
        ev.setLocation(mOnTouchEventWorkingArray[0], mOnTouchEventWorkingArray[1]);
        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;
                mLastTouchY = y;
                // Save the ID of this pointer
                mActivePointerId = ev.getPointerId(0);
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                // Find the index of the active pointer and fetch its position
                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
                final float x = ev.getX(pointerIndex);
                final float y = ev.getY(pointerIndex);
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;
                mTranslateMatrix.preTranslate(dx, dy);
                mTranslateMatrix.invert(mTranslateMatrixInverse);
                mLastTouchX = x;
                mLastTouchY = y;
                normal = false;
                invalidate();
                break;
            }
            case MotionEvent.ACTION_UP: {
                mActivePointerId = INVALID_POINTER_ID;
                break;
            }
            case MotionEvent.ACTION_CANCEL: {
                mActivePointerId = INVALID_POINTER_ID;
                break;
            }
            case MotionEvent.ACTION_POINTER_UP: {
                // Extract the index of the pointer that left the touch sensor
                final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final int pointerId = ev.getPointerId(pointerIndex);
                if (pointerId == mActivePointerId) {
                    // This was our active pointer going up. Choose a new
                    // active pointer and adjust accordingly.
                    final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                    mLastTouchX = ev.getX(newPointerIndex);
                    mLastTouchY = ev.getY(newPointerIndex);
                    mActivePointerId = ev.getPointerId(newPointerIndex);
                }
                break;
            }
        }
    }
    return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        if (detector.isInProgress()) {
            mFocusX = detector.getFocusX();
            mFocusY = detector.getFocusY();
        }
        mScaleFactor = Math.max(0.75f, Math.min(mScaleFactor, 3.0f));
        mScaleMatrix.setScale(mScaleFactor, mScaleFactor,
                mFocusX, mFocusY);
        mScaleMatrix.invert(mScaleMatrixInverse);
        invalidate();
        requestLayout();
        return true;
    }
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    // event when double tap occurs
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        if (!normal) {
            restore();
        } else {
            zoomToSpot(e);
        }
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return true;
    }
}

所以我让我的代码使用这些小东西,首先我添加了双击boolean

public boolean onDoubleTap(MotionEvent e) {
        if (!normal) {
            restore();
        } else {
            zoomToSpot(e);
        }
        doubleclicked = true;
        return true;
    }

然后我做了两个不同的onTouchEvents和设置第一个只工作,如果双击是假的。当屏幕上没有手指时,第二个选项将设置双击为false。

public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    if (!doubleclicked) {
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                //something...
        }
    } else {
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP: {
                doubleclicked = false;
                break;
            }
         }
     }

相关内容

  • 没有找到相关文章

最新更新