MuPDF Android PDF适应屏幕

4

我成功地为我的一个Android应用程序安装了MuPDF。但问题是,在渲染时,我无法将PDF适配到屏幕上。请问有人可以建议我如何实现这一点吗?谢谢!

4个回答

4
将ReaderView.java中的measureView方法编辑为以下内容。
private void measureView(View v) {
        // See what size the view wants to be
        v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // Work out a scale that will fit it to this view
        float scale;
        if (getWidth()>getHeight())
        {
            scale = (float)getWidth()/(float)v.getMeasuredWidth();
            MIN_SCALE = (float)v.getMeasuredWidth()/(float)getWidth();
            MAX_SCALE = MIN_SCALE*5.0f;
        }
        else
            scale = Math.min((float)getWidth()/(float)v.getMeasuredWidth(),
                        (float)getHeight()/(float)v.getMeasuredHeight());

        // Use the fitting values scaled by our current scale factor
        v.measure(View.MeasureSpec.EXACTLY | (int)(v.getMeasuredWidth()*scale*mScale),
                View.MeasureSpec.EXACTLY | (int)(v.getMeasuredHeight()*scale*mScale));
    }

这个解决方案的唯一问题是它不能正确地渲染初始pdf视图到新计算的比例尺(它被渲染为1.0f比例尺)。只有在进行一些交互(触摸,平移,缩放)之后,它才能正确地呈现。是否有任何方法可以强制初始渲染到新比例尺? - Bruno
这是一篇旧帖子,不能再次浏览它。 - hasan

1

我在使用MAX和MIN比例的变量时遇到了错误,但是我对@hassan83的解决方案进行了轻微修改。我的解决方案已在MuPDF 1.9a版本中进行了测试。

private void measureView(View v) {
    // See what size the view wants to be
    v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    // Work out a scale that will fit it to this view
    float scale;
    //landscape orientation
    if (getWidth() > getHeight())
    {
        scale = (float)getWidth()/(float)v.getMeasuredWidth() * (1.0f ); //make sure that we fill the page by multiplying with a scale factor of one
    }
    else
        scale = Math.min((float)getWidth()/(float)v.getMeasuredWidth(),
                (float)getHeight()/(float)v.getMeasuredHeight());

    // Use the fitting values scaled by our current scale factor
    v.measure(View.MeasureSpec.EXACTLY | (int)(v.getMeasuredWidth()*scale*mScale),
            View.MeasureSpec.EXACTLY | (int)(v.getMeasuredHeight()*scale*mScale));
}

0

使用这个方法,页面可以从视图中调整大小。下面是我的代码中的“measureview”方法:

    private void measureView(View v) {
    // See what size the view wants to be
    v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

    if (!mReflow) {
        // Work out a scale that will fit it to this view
        float scale = Math.min((float) getWidth() / (float) v.getMeasuredWidth(),
                (float) getHeight() / (float) v.getMeasuredHeight());
        // Use the fitting values scaled by our current scale factor
        v.measure(MeasureSpec.AT_MOST | (int) (v.getMeasuredWidth() * (scale + 0.8f) * mScale),
                MeasureSpec.AT_MOST | (int) (v.getMeasuredHeight() * (scale+0.8f) * mScale));
    } else {
        v.measure(View.MeasureSpec.EXACTLY | (int) (v.getMeasuredWidth()),
                View.MeasureSpec.EXACTLY | (int) (v.getMeasuredHeight()));
    }
}

这是我需要的,页面启动时需要精确获取视图中的内容,并且带有可读的文本。


-1
除了回答https://dev59.com/C3vaa4cB1Zd3GeqPADBu#21168243之外,你可以在结尾处添加以下代码重新渲染视图:
requestLayout();
post(this);

再次。

因此,完整的代码如下(上一条评论之前的代码归属权:本问题的被接受答案):

private void measureView(View v) {
        // See what size the view wants to be
        v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // Work out a scale that will fit it to this view
        float scale;
        if (getWidth()>getHeight())
        {
            scale = (float)getWidth()/(float)v.getMeasuredWidth();
            MIN_SCALE = (float)v.getMeasuredWidth()/(float)getWidth();
            MAX_SCALE = MIN_SCALE*5.0f;
        }
        else
            scale = Math.min((float)getWidth()/(float)v.getMeasuredWidth(),
                        (float)getHeight()/(float)v.getMeasuredHeight());

        // Use the fitting values scaled by our current scale factor
        v.measure(View.MeasureSpec.EXACTLY | (int)(v.getMeasuredWidth()*scale*mScale),
                View.MeasureSpec.EXACTLY | (int)(v.getMeasuredHeight()*scale*mScale));

        // last comment
        // to force the initial rendering to the new scale

        requestLayout();
        post(this);
    }

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接