【问题标题】:MuPDF Android Pdf fit to the screenMuPDF Android Pdf 适合屏幕
【发布时间】:2014-01-30 12:42:13
【问题描述】:

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

【问题讨论】:

    标签: android mupdf


    【解决方案1】:

    编辑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 比例渲染的)。只有在进行了一些交互(触摸、平移、缩放)之后,它才会正确渲染。有什么方法可以强制初始渲染到新的比例?
    • 这是一个老帖子,不能再扔了。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      这样,页面的大小从视图中引导过大。这是我的代码中的“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()));
          }
      }
      

      这是我需要的,启动页面需要在视图中准确获取,启动时文本可读。

      【讨论】:

        【解决方案4】:

        除了回答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);
            }
        

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-05-21
          • 1970-01-01
          • 2017-07-05
          • 2012-05-02
          • 1970-01-01
          • 2016-04-22
          • 1970-01-01
          • 2021-05-23
          相关资源
          最近更新 更多