问题
I succesfully installed MuPDF for one of my Android Apps. But the problems is, while rendering I cannot fit the PDF to the screen. Can anybody please suggest me how to achieve this. Thanks!
回答1:
Edit the measureView method in ReaderView.java to become.
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));
}
回答2:
I was getting errors with regard to the MAX and MIN scale final variables, however I made a slight modification to @hassan83's solution. My solution is tested in MuPDF version 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:
with this the page is resized boot too larger from the view. here is the "measureview" method in my code:
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()));
}
}
this is what I need, boot the page need to be fetched in exactly in the view, boot with the text readable.
回答4:
In addition to answer https://stackoverflow.com/a/21168243/2982225, you can re-render the view by adding the following code at the end:
requestLayout();
post(this);
again.
So the complete code is as follows (attribution for code before last comment: 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);
}
Hope this helps.
来源:https://stackoverflow.com/questions/21063005/mupdf-android-pdf-fit-to-the-screen