MuPDF Android Pdf适合屏幕

时间:2014-01-11 13:45:41

标签: android mupdf

我为我的一个Android应用程序成功安装了MuPDF。但问题是,虽然渲染我无法将PDF放到屏幕上。任何人都可以建议我如何实现这一目标。谢谢!

4 个答案:

答案 0 :(得分: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));
    }

答案 1 :(得分: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));
}

答案 2 :(得分: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()));
    }
}

这就是我所需要的,启动页面需要在视图中完全取出,启动文本可读。

答案 3 :(得分:-1)

除了回答https://stackoverflow.com/a/21168243/2982225, 您可以通过在末尾添加以下代码来重新呈现视图:

requestLayout();
post(this);

试。

所以完整的代码如下(最后评论前的代码归属:the accepted answer of this question):

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);
    }

希望这有帮助。