Skip to content Skip to sidebar Skip to footer

Mupdf Android Library Vertical Scroll

I am using MuPdf Android Library, I want vertical Scroll instead of horizontal page Scrolling, tried everything but failed, googled the problem and found an answer on Stackoverflow

Solution 1:

As Arunjyothis said :

You can do by changing the horizontal values to vertical ( change all width related calculations to height )

Check the answer HERE.

Solution 2:

I created new binding library for Mupdf with method swipeHorizontal(boolean isHorizontal) so you can easy change between vertical and horizontal scroll

https://github.com/minaairsupport/MuPDF_Xamarin

Just download the library and reference it in your project

Solution 3:

You can do by changing the horizontal values to vertical ( change all width related calculations to height ). Change onLayout() & onFling() as below: (Reference)

@OverrideprotectedvoidonLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        // "Edit mode" means when the View is being displayed in the Android GUI editor. (this class// is instantiated in the IDE, so we need to be a bit careful what we do).if (isInEditMode())
            return;

        Viewcv= mChildViews.get(mCurrent);
        Point cvOffset;

        if (!mResetLayout) {
            // Move to next or previous if current is sufficiently off centerif (cv != null) {
                cvOffset = subScreenSizeOffset(cv);
                // cv.getRight() may be out of date with the current scale// so add left to the measured width for the correct position//if (cv.getLeft() + cv.getMeasuredWidth() + cvOffset.x + GAP/2 + mXScroll < getWidth()/2 && mCurrent + 1 < mAdapter.getCount()) {if (cv.getTop() + cv.getMeasuredHeight() + cvOffset.y + GAP / 2 + mYScroll < getHeight() / 2 && mCurrent + 1 < mAdapter.getCount()) {
                    postUnsettle(cv);
                    // post to invoke test for end of animation// where we must set hq area for the new current view
                    mStepper.prod();

                    onMoveOffChild(mCurrent);
                    mCurrent++;
                    onMoveToChild(mCurrent);
                }

                //if (cv.getLeft() - cvOffset.x - GAP/2 + mXScroll >= getWidth()/2 && mCurrent > 0) {if (cv.getTop() - cvOffset.y - GAP / 2 + mYScroll >= getHeight() / 2 && mCurrent > 0) {
                    postUnsettle(cv);
                    // post to invoke test for end of animation// where we must set hq area for the new current view
                    mStepper.prod();

                    onMoveOffChild(mCurrent);
                    mCurrent--;
                    onMoveToChild(mCurrent);
                }
            }

            // Remove not needed children and hold them for reuseintnumChildren= mChildViews.size();
            int childIndices[] = newint[numChildren];
            for (inti=0; i < numChildren; i++)
                childIndices[i] = mChildViews.keyAt(i);

            for (inti=0; i < numChildren; i++) {
                intai= childIndices[i];
                if (ai < mCurrent - 1 || ai > mCurrent + 1) {
                    Viewv= mChildViews.get(ai);
                    onNotInUse(v);
                    mViewCache.add(v);
                    removeViewInLayout(v);
                    mChildViews.remove(ai);
                }
            }
        } else {
            mResetLayout = false;
            mXScroll = mYScroll = 0;

            // Remove all children and hold them for reuseintnumChildren= mChildViews.size();
            for (inti=0; i < numChildren; i++) {
                Viewv= mChildViews.valueAt(i);
                onNotInUse(v);
                mViewCache.add(v);
                removeViewInLayout(v);
            }
            mChildViews.clear();

            // Don't reuse cached views if the adapter has changedif (mReflowChanged) {
                mReflowChanged = false;
                mViewCache.clear();
            }

            // post to ensure generation of hq area
            mStepper.prod();
        }

        // Ensure current view is presentint cvLeft, cvRight, cvTop, cvBottom;
        booleannotPresent= (mChildViews.get(mCurrent) == null);
        cv = getOrCreateChild(mCurrent);
        // When the view is sub-screen-size in either dimension we// offset it to center within the screen area, and to keep// the views spaced out
        cvOffset = subScreenSizeOffset(cv);
        if (notPresent) {
            //Main item not already present. Just place it top left
            cvLeft = cvOffset.x;
            cvTop = cvOffset.y;
        } else {
            // Main item already present. Adjust by scroll offsets
            cvLeft = cv.getLeft() + mXScroll;
            cvTop = cv.getTop() + mYScroll;
        }
        // Scroll values have been accounted for
        mXScroll = mYScroll = 0;
        cvRight = cvLeft + cv.getMeasuredWidth();
        cvBottom = cvTop + cv.getMeasuredHeight();

        if (!mUserInteracting && mScroller.isFinished()) {
            Pointcorr= getCorrection(getScrollBounds(cvLeft, cvTop, cvRight, cvBottom));
            cvRight += corr.x;
            cvLeft += corr.x;
            cvTop += corr.y;
            cvBottom += corr.y;
        } elseif (cv.getMeasuredWidth() <= getWidth()) {
//            // When the current view is as small as the screen in height, clamp//            // it vertically//            Point corr = getCorrection(getScrollBounds(cvLeft, cvTop, cvRight, cvBottom));//            cvTop += corr.y;//            cvBottom += corr.y;// When the current view is as small as the screen in width, clamp// it horizontallyPointcorr= getCorrection(getScrollBounds(cvLeft, cvTop, cvRight, cvBottom));
            cvRight += corr.x;
            cvLeft += corr.x;
        }



        cv.layout(cvLeft, cvTop, cvRight, cvBottom);

        if (mCurrent > 0) {
            Viewlv= getOrCreateChild(mCurrent - 1);
            PointleftOffset= subScreenSizeOffset(lv);
            /*int gap = leftOffset.x + GAP + cvOffset.x;
            lv.layout(cvLeft - lv.getMeasuredWidth() - gap,
                    (cvBottom + cvTop - lv.getMeasuredHeight())/2,
                    cvLeft - gap,
                    (cvBottom + cvTop + lv.getMeasuredHeight())/2);*/intgap= leftOffset.y + GAP + cvOffset.y;
            lv.layout((cvRight + cvLeft - lv.getMeasuredWidth())/2,
                    cvTop - lv.getMeasuredHeight() - gap,
                    (cvRight + cvLeft + lv.getMeasuredWidth())/2,
                    cvTop - gap);

        }

        if (mCurrent + 1 < mAdapter.getCount()) {
            Viewrv= getOrCreateChild(mCurrent + 1);
            PointrightOffset= subScreenSizeOffset(rv);
            /*int gap = cvOffset.x + GAP + rightOffset.x;
            rv.layout(cvRight + gap,
                    (cvBottom + cvTop - rv.getMeasuredHeight())/2,
                    cvRight + rv.getMeasuredWidth() + gap,
                    (cvBottom + cvTop + rv.getMeasuredHeight())/2);*/intgap= cvOffset.y + GAP + rightOffset.y;
            rv.layout((cvRight + cvLeft - rv.getMeasuredWidth())/2,
                    cvBottom + gap,
                    (cvRight + cvLeft + rv.getMeasuredWidth())/2,
                    cvBottom + rv.getMeasuredHeight() + gap);



        }

        invalidate();
    }

Also add the below lines:

publicbooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        if (mScaling)
            returntrue;

        Viewv= mChildViews.get(mCurrent);
        if (v != null) {
            Rectbounds= getScrollBounds(v);
            switch (directionOfTravel(velocityX, velocityY)) {

                case MOVING_UP:
                    if (bounds.top >= 0) {
                        // Fling off to the left bring next view onto screenViewvl= mChildViews.get(mCurrent + 1);

                        if (vl != null) {
                            slideViewOntoScreen(vl);
                            returntrue;
                        }
                    }
                    break;

                case MOVING_DOWN:
                    if (bounds.bottom <= 0) {
                        // Fling off to the right bring previous view onto screenViewvr= mChildViews.get(mCurrent - 1);

                        if (vr != null) {
                            slideViewOntoScreen(vr);
                            returntrue;
                        }
                    }
                    break;
            }
            mScrollerLastX = mScrollerLastY = 0;
            // If the page has been dragged out of bounds then we want to spring back// nicely. fling jumps back into bounds instantly, so we don't want to use// fling in that case. On the other hand, we don't want to forgo a fling// just because of a slightly off-angle drag taking us out of bounds other// than in the direction of the drag, so we test for out of bounds only// in the direction of travel.//// Also don't fling if out of bounds in any direction by more than fling// marginRectexpandedBounds=newRect(bounds);
            expandedBounds.inset(-FLING_MARGIN, -FLING_MARGIN);

            if (withinBoundsInDirectionOfTravel(bounds, velocityX, velocityY)
                    && expandedBounds.contains(0, 0)) {
                mScroller.fling(0, 0, (int) velocityX, (int) velocityY, bounds.left, bounds.right, bounds.top, bounds.bottom);
                mStepper.prod();
            }
        }

        returntrue;
    }

Post a Comment for "Mupdf Android Library Vertical Scroll"