Skip to content Skip to sidebar Skip to footer

Setting Boundary Limits For Panning/dragging A Zoomable Custom Relative Layout

I've implemented zoom and pan/drag functionality in my custom relative layout successfully with the help of following links. 1). An excellent tutorial - making-sense-of-multitouch

Solution 1:

You need to put min/max limits on the mPosX & mPosY positions - do this after adding dx & dy.

To work out what limits to apply you could get the child View's size using getWidth() and getHeight(), and factor in the current scale.

Solution 2:

I've somehow found a workaround to this by some hooks, which is far from being a perfect solution, since I had to hard-code some values. I've added the following code-snippet in my existing code to set the boundary limits to panning. Now the panning works fine.

Logging the mClipBound.top, bottom, left, right values I came up with the following hard-coded values which might work only for 720x1280 & 800x1280 devices.

case MotionEvent.ACTION_MOVE: {
    ...
    ...

    if (isPortraitMode) {
        if (mClipBound.top < 104 && dy > 0) //104 is a hard-coded value here.
           dy = 0;

        if (mClipBound.bottom > (screenHeight - 77) && dy < 0) // 77 - hardcoded
           dy = 0;
    } else {
        if (mClipBound.top < 0 && dy > 0)
            dy = 0;
        if (mClipBound.bottom > screenHeight && dy < 0)
        dy = 0;

    }

    if (mClipBound.left < 30 && dx > 0) // 30 - hardcoded
        dx = 0;

    if (mClipBound.right > screenWidth && dx < 0)
        dx = 0;

    mPosX += dx;
    mPosY += dy;
        ...
        ...
        ...
 }

 ...
 ...


 @OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    intwidthSize= MeasureSpec.getSize(widthMeasureSpec);
    intheightSize= MeasureSpec.getSize(heightMeasureSpec);
    screenWidth = widthSize;
    screenHeight = heightSize;
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension((int) (widthSize), (int) (heightSize ));
 }

But still i haven't been able to resolve problem 2: mentioned in my question above. Some proper adjustments in onMeasure(..) & onLayout(..) methods using mScaleFactor might resolve the issue. But i haven't been able to make it work yet.

Solution 3:

For the problem 2, you have to resize the canvas rect of all your relativelayout´s children. You should do this in onScaleEnd method of your scalegesturedetector.

publicvoidresizeChildren() {

    int childCount = this.getChildCount();

    for (int i = 0; i < childCount; i++) {

        MyView child = (MyView)this.getChildAt(i);
        child.setScaleFactor(mScaleFactor);
        child.setCanvasRect(mRect);


    }

}

setScaleFactor() and setCanvasRect() are methods for MyView which is a custom View I use for finger painting.

Post a Comment for "Setting Boundary Limits For Panning/dragging A Zoomable Custom Relative Layout"