2014-01-11 143 views
4

我成功地爲我的Android應用安裝了MuPDF。但問題是,在渲染時,我無法使PDF適合屏幕。任何人都可以請建議我如何實現這一點。謝謝!MuPDF Android Pdf適合屏幕

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

這個解決方案的唯一的問題是,它會無法正常顯示的初始PDF瀏覽到新的計算規模(它呈現爲1.0F規模)。只有在做了一些交互(觸摸,平移,縮放)之後,它才能正確渲染。任何方法強制初始渲染到新的尺度? – Bruno

+0

這是一箇舊帖子,不能再去扔它。 – hasan83

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

希望這有助於。

0

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