RadaeePDF.com :: Topic: How to avoid page change during navigation if page (1/1)
Welcome, Guest
Username: Password: Remember me

Signin/Signup with:

Questions about Android development and PDF

TOPIC: How to avoid page change during navigation if page

How to avoid page change during navigation if page 2 years 3 months ago #8763

  • asalem57
  • asalem57's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 1
  • Thank you received: 1
  • Karma: 0
This is the code i used to prevent the PDF from flipping while the page is zoomed in

i used www.radaeepdf.com/support?view=kb&kbartid=15 as a reference but i added support for RTL and extended the view class instead of replacing it
package com.radaee.view;

import com.radaee.pdf.Document;
import com.radaee.pdf.Global;
import com.radaee.view.PDFView.PDFPos;
import com.radaee.view.PDFView.PDFViewListener;

import android.content.Context;
import android.content.res.Configuration;
import android.view.MotionEvent;

public class PDFViewDualExt extends PDFViewDual {

    protected boolean isRTL = false;

    Context context;

    public PDFViewDualExt(Context context, boolean isRTL) {
        super(context);
        this.context = context;
        this.isRTL = isRTL;
    }


    @Override
    protected boolean motionNormal(MotionEvent event) {
        super.motionNormal(event);
        try {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_MOVE:
                    if (m_status == STA_MOVING) {
                        int x = m_scroller.getFinalX();
                        if (x + m_w > m_docw) x = m_docw - m_w;
                        if (vGetScale() > vGetMinScale()) {
                            try {
                                if (Global.def_view == 6 && m_w > m_h && isLandscape()) { //dual page
                                    if (isRTL) {
                                        if ((m_pageno - 2 >= 0) && (m_pageno - 2 < m_pages.length) && x + m_w > m_pages[m_pageno - 2].m_x)
                                            x = m_pages[m_pageno - 2].m_x - m_w;
                                        if (m_pageno >= 0 && x <= (m_pages[m_pageno + 1].m_x + m_pages[m_pageno + 1].m_w))
                                            x = (m_pages[m_pageno + 1].m_x + m_pages[m_pageno + 1].m_w);
                                    } else {
                                        if ((m_pageno + 2 < m_pages.length) && x + m_w > m_pages[m_pageno + 2].m_x)
                                            x = m_pages[m_pageno + 2].m_x - m_w;
                                        if (m_pageno > 0 && x <= (m_pages[m_pageno - 1].m_x + m_pages[m_pageno - 1].m_w))
                                            x = (m_pages[m_pageno - 1].m_x + m_pages[m_pageno - 1].m_w);
                                    }
                                } else { //single page
                                    if (isRTL) {
                                        if ((m_pageno - 1) >= 0 && (m_pageno - 1 < m_pages.length) && x + m_w > m_pages[m_pageno - 1].m_x)
                                            x = m_pages[m_pageno - 1].m_x - m_w;
                                        if (m_pageno >= 0 && x <= (m_pages[m_pageno + 1].m_x + m_pages[m_pageno + 1].m_w))
                                            x = (m_pages[m_pageno + 1].m_x + m_pages[m_pageno + 1].m_w);
                                    } else {
                                        if ((m_pageno + 1 < m_pages.length) && x + m_w > m_pages[m_pageno + 1].m_x)
                                            x = m_pages[m_pageno + 1].m_x - m_w;
                                        if (m_pageno > 0 && x <= (m_pages[m_pageno - 1].m_x + m_pages[m_pageno - 1].m_w))
                                            x = (m_pages[m_pageno - 1].m_x + m_pages[m_pageno - 1].m_w);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        if (x < 0) x = 0;
                        m_scroller.setFinalX(x);
                        if (m_listener != null) m_listener.OnPDFInvalidate(false);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    if (m_status == STA_MOVING) {
                        int x = m_scroller.getFinalX();
                        if (x + m_w > m_docw) x = m_docw - m_w;

                        if (vGetScale() > vGetMinScale()) {
                            try {
                                if (Global.def_view == 6 && m_w > m_h && isLandscape()) { //dual page
                                    if (isRTL) {
                                        if ((m_pageno - 2 >= 0) && (m_pageno - 2 < m_pages.length) && x + m_w > m_pages[m_pageno - 2].m_x)
                                            x = m_pages[m_pageno - 2].m_x - m_w;
                                        if (m_pageno >= 0 && x <= (m_pages[m_pageno + 1].m_x + m_pages[m_pageno + 1].m_w))
                                            x = (m_pages[m_pageno + 1].m_x + m_pages[m_pageno + 1].m_w);
                                    } else {
                                        if ((m_pageno + 2 < m_pages.length) && x + m_w > m_pages[m_pageno + 2].m_x)
                                            x = m_pages[m_pageno + 2].m_x - m_w;
                                        if (m_pageno > 0 && x <= (m_pages[m_pageno - 1].m_x + m_pages[m_pageno - 1].m_w))
                                            x = (m_pages[m_pageno - 1].m_x + m_pages[m_pageno - 1].m_w);
                                    }
                                } else { //single page
                                    if (isRTL) {
                                        if ((m_pageno - 1 >= 0) && (m_pageno - 1 < m_pages.length) && x + m_w > m_pages[m_pageno - 1].m_x)
                                            x = m_pages[m_pageno - 1].m_x - m_w;
                                        if (m_pageno >= 0 && x <= (m_pages[m_pageno + 1].m_x + m_pages[m_pageno + 1].m_w))
                                            x = (m_pages[m_pageno + 1].m_x + m_pages[m_pageno + 1].m_w);
                                    } else {
                                        if ((m_pageno + 1 < m_pages.length) && x + m_w > m_pages[m_pageno + 1].m_x)
                                            x = m_pages[m_pageno + 1].m_x - m_w;
                                        if (m_pageno > 0 && x <= (m_pages[m_pageno - 1].m_x + m_pages[m_pageno - 1].m_w))
                                            x = (m_pages[m_pageno - 1].m_x + m_pages[m_pageno - 1].m_w);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        if (x < 0) x = 0;
                        m_scroller.setFinalX(x);
                        m_status = STA_NONE;
                        vOnMoveEnd(x, m_scroller.getFinalY());
                        if (m_listener != null) m_listener.OnPDFInvalidate(false);
                    }
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean isLandscape() {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    @Override
    protected boolean vOnFling(float dx, float dy, float velocityX, float velocityY) {
        if (vGetScale() > vGetMinScale()) return true;
        return super.vOnFling(dx, dy, velocityX, velocityY);
    }

/**
     * This override is to prevent scroller from stopping between two pages while searching
     */
    @Override
    protected void vFindGoto() {
        if (m_pages == null) return;
        int pg = m_finder.find_get_page();
        if (pg < 0 || pg >= m_doc.GetPageCount()) return;
        float pos[] = m_finder.find_get_pos();
        if (pos == null) return;
        pos[0] = m_pages[pg].ToDIBX(pos[0]) + m_pages[pg].GetX();
        pos[1] = m_pages[pg].ToDIBY(pos[1]) + m_pages[pg].GetY();
        pos[2] = m_pages[pg].ToDIBX(pos[2]) + m_pages[pg].GetX();
        pos[3] = m_pages[pg].ToDIBY(pos[3]) + m_pages[pg].GetY();
        float x = m_scroller.getCurrX();
        float y = m_scroller.getCurrY();
        if (x > pos[0] - m_w / 8) x = pos[0] - m_w / 8;
        if (x < pos[2] - m_w * 7 / 8) x = pos[2] - m_w * 7 / 8;
        if (y > pos[1] - m_h / 8) y = pos[1] - m_h / 8;
        if (y < pos[3] - m_h * 7 / 8) y = pos[3] - m_h * 7 / 8;
        if (x > m_docw - m_w) x = m_docw - m_w;
        if (x < 0) x = 0;
        if (y > m_doch - m_h) y = m_doch - m_h;
        if (y < 0) y = 0;
        m_scroller.forceFinished(true);
        m_scroller.setFinalX((int) x);
        m_scroller.setFinalY((int) y);
        if (m_listener != null) m_listener.OnPDFInvalidate(false);
        /*center page after search*/

        if (vGetScale() <= vGetMinScale()) {

            vCenterPage(pg);
        }
    }
}
Last Edit: 2 years 3 months ago by asalem57.
The administrator has disabled public write access.
The following user(s) said Thank You: support
Powered by Kunena Forum