Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid issue: View with endless scrolling
    primarykey
    data
    text
    <p>I have a custom View with scroll implemented, but it seems like an ENDLESS scrolling over the image.<br> Even when I find the edge of the image, it keeps scrolling to a blank background.</p> <p>I can't use a WebView because I also have some Canvas sutff.<br> Does anyone know how to set limits for this issue? <br> How to fit the edges of the image for the scrolling?<br> <br><br> <strong>EDIT:</strong> I found the best solution with @JosephEarl help.<br> I set just left and top bounds because my image is larger than the screen.<br> Also I <em>turn off</em> the bounds while using the zoom funcionality, otherwise I couldn't move it anymore.<br><br> <strong>1) In the ACTION_MOVE case of your <em>onTouch</em> event, inser this code:</strong></p> <pre><code>if(!isZoomed) { if(mPosX &lt; 0) mPosX = 0; else if(mPosX &gt; mWidth) mPosX = mWidth; if(mPosY &lt; 0) mPosY = 0; else if(mPosY &gt; mHeight) mPosY = mHeight; } </code></pre> <p><br><strong>2) <em>Turn on</em> or <em>turn off</em> the bounds while zoom is used.<br> Add the following code to your ACTION_POINTER_UP case:</strong></p> <pre><code>case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = (ev.getAction() &amp; MotionEvent.ACTION_POINTER_INDEX_MASK) &gt;&gt; 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; } </code></pre> <p><br> And that's all. <br> Here is all the related methods and complete <em>onTouch</em> event:<br></p> <pre><code>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 &amp; 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 &lt; 0) mPosX = 0; else if(mPosX &gt; mWidth) mPosX = mWidth; if(mPosY &lt; 0) mPosY = 0; else if(mPosY &gt; 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() &amp; MotionEvent.ACTION_POINTER_INDEX_MASK) &gt;&gt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload