我有一个带有滚动的自定义视图,但它看起来像是一个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;
}
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;
}
就这样。
以下是所有相关方法和完整的 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
答案 0 :(得分:2)
您的代码似乎在任何时候都没有尝试检测可滚动区域的大小并将位置限制为这些边界,即您应该检查以确保mPosX
和mPosY
做不超过他们的界限。
系统不会自动限制自定义视图的位置,这是您自己需要做的事情。
顶部和左侧边界将为0,0 - 您应该确保mPosX
和mPosY
不大于此值。
右边界将是(容器视图的宽度 - 滚动视图的宽度) - 这应该是负数(如果它更大,则将右边界设置为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;
}
imageWidth
和imageHeight
是您滚动的任何内容的宽度和高度。
然后在滚动时确保遵守边界:
if (mPosX > leftBound) {
mPosX = leftBound;
} else if (mPosX < rightBound) {
mPosX = rightBound;
}
if (mPosY > topBound) {
mPosY = topBound;
} else if (mPosY < bottomBound) {
mPosY = bottomBound;
}