Android问题:无限滚动查看

Ray*_*ian 6 android scroll limits image edges

我有一个自定义视图与滚动实现,但它看起来像ENDLESS滚动图像.
即使我找到图像的边缘,它仍然滚动到空白背景.

我不能使用WebView,因为我也有一些Canvas sutff.
有谁知道如何为这个问题设置限制?
如何适应滚动的图像边缘?


编辑:我找到了@JosephEarl帮助的最佳解决方案.
我设置了左边和上边界,因为我的图像比屏幕大.
此外,我在使用缩放功能时关闭边界,否则我无法移动它.

1)在onTouch事件的ACTION_MOVE情况下,输入以下代码:

if(!isZoomed) {
    if(mPosX < 0)
        mPosX = 0;
    else if(mPosX > mWidth)
        mPosX = mWidth;
    if(mPosY < 0)
        mPosY = 0;
    else if(mPosY > mHeight)
        mPosY = mHeight;
}
Run Code Online (Sandbox Code Playgroud)


2)使用变焦时打开关闭边界.
将以下代码添加到ACTION_POINTER_UP案例中:

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);
        isZoomed = true;

    } else
        isZoomed = false;

    break;

}
Run Code Online (Sandbox Code Playgroud)


就这样.
以下是所有相关方法并完成onTouch事件:

private float scaleFactor = 1.f;
private ScaleGestureDetector detector;

private static final int INVALID_POINTER_ID = -1;
private int mActivePointerId = INVALID_POINTER_ID;

private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;

private float mWidth;
private float mHeight;
private boolean isZoomed = false;

// OTHER CODE GOES HERE

@Override
public boolean onTouchEvent(MotionEvent ev) {
    detector.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;
            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 (!detector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;
                mPosX += dx;
                mPosY += dy;

                if(!isZoomed) {
                    if(mPosX < 0)
                        mPosX = 0;
                    else if(mPosX > mWidth)
                        mPosX = mWidth;
                    if(mPosY < 0)
                        mPosY = 0;
                    else if(mPosY > mHeight)
                        mPosY = mHeight;
                }

                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;

            break;
        }

        case MotionEvent.ACTION_UP: {
            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);
                isZoomed = true;

            } else
                isZoomed = false;

            break;

        }
    }

    return true;
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        scaleFactor *= detector.getScaleFactor();
        scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
        invalidate();
        return true;
    }
}

@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
     super.onSizeChanged(xNew, yNew, xOld, yOld);
     mWidth = xNew;
     mHeight = yNew;
}

// OTHER CODE GOES HERE
Run Code Online (Sandbox Code Playgroud)

Jos*_*arl 2

从您的代码看来,您在任何时候都不会尝试检测可滚动区域的大小并将位置限制在这些边界内,即您应该检查以确保mPosXmPosY超出其边界。

系统不会自动限制你的自定义View的位置,这是你需要自己做的事情。

上界和左界将为 0, 0——您应该确保mPosXmPosY不大于此值。

右边界将是(容器视图的宽度 - 滚动视图的宽度)——这应该是负数(如果它大于则将右边界设置为 0)并且您应该确保mPosX不小于此值。底部边界将为(容器的高度 - 滚动视图的高度)——这又应该是负数,并且您应该确保mPosY不小于该值。

总结一下,当触摸事件开始计算边界时:

// Calculate our bounds.
int leftBound = 0;
int topBound = 0;
int rightBound = imageWidth - getWidth();
if (rightBound > 0) {
    rightBound = 0;
}
int bottomBound = imageHeight - getHeight();
if (bottomBound > 0) {
    bottomBound = 0;
}
Run Code Online (Sandbox Code Playgroud)

其中imageWidthimageHeight是您滚动的内容的宽度和高度。

然后在滚动时确保遵守边界:

if (mPosX > leftBound) {
    mPosX = leftBound;
} else if (mPosX < rightBound) {
    mPosX = rightBound;
}

if (mPosY > topBound) {
    mPosY = topBound;
} else if (mPosY < bottomBound) {
    mPosY = bottomBound;
}
Run Code Online (Sandbox Code Playgroud)