gpt4 book ai didi

安卓问题 : View with endless scrolling

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:17:42 26 4
gpt4 key购买 nike

我有一个实现了滚动的自定义 View ,但它似乎在图像上无休止地滚动。
即使我找到图像的边缘,它也会一直滚动到空白背景。

我不能使用 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

最佳答案

从您的代码看来,您绝不会尝试检测可滚动区域的大小并将位置限制在这些范围内,即您应该检查以确保 mPosX mPosY 不要超出他们的界限。

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

顶部和左侧边界将为 0、0——您应确保 mPosXmPosY 不大于此值。

右边界将是(容器 View 的宽度 - ScrollView 的宽度)——这应该是负数(如果它大于则将右边界设置为 0)并且你应该确保 mPosX 不少于此。底部边界将是(容器的高度 - ScrollView 的高度)——这又应该是负数,您应该确保 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;
}

imageWidthimageHeight 是您正在滚动的内容的宽度和高度。

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

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

if (mPosY > topBound) {
mPosY = topBound;
} else if (mPosY < bottomBound) {
mPosY = bottomBound;
}

关于安卓问题 : View with endless scrolling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13483386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com