scale properly

This commit is contained in:
contrudar 2016-10-05 23:26:41 +03:00
parent 824d7a585a
commit 838b2cfcbf
2 changed files with 137 additions and 10 deletions

View File

@ -16,6 +16,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@ -120,12 +121,9 @@ public class PdfViewerFragment extends Fragment {
return mGraphView;
} else {
mGraphView = new GraphView(getActivity());
if (savedInstanceState != null) {
PDFImage.sShowImages = savedInstanceState.getBoolean(PdfViewerFragment.EXTRA_SHOWIMAGES, PdfViewerFragment.DEFAULTSHOWIMAGES);
PDFPaint.s_doAntiAlias = savedInstanceState.getBoolean(PdfViewerFragment.EXTRA_ANTIALIAS, PdfViewerFragment.DEFAULTANTIALIAS);
PDFFont.sUseFontSubstitution = savedInstanceState.getBoolean(PdfViewerFragment.EXTRA_USEFONTSUBSTITUTION,
PdfViewerFragment.DEFAULTUSEFONTSUBSTITUTION);
}
PDFImage.sShowImages = PdfViewerFragment.DEFAULTSHOWIMAGES;
PDFPaint.s_doAntiAlias = PdfViewerFragment.DEFAULTANTIALIAS;
PDFFont.sUseFontSubstitution = PdfViewerFragment.DEFAULTUSEFONTSUBSTITUTION;
HardReference.sKeepCaches = true;
mPage = STARTPAGE;
@ -598,7 +596,6 @@ public class PdfViewerFragment extends Fragment {
private void showPage(int page) {
try {
// free memory from previous page
mGraphView.setPageBitmap(null);
mGraphView.updateImage();
@ -608,14 +605,44 @@ public class PdfViewerFragment extends Fragment {
mPdfPage = mPdfFile.getPage(page, true);
}
final Bitmap bitmap = mPdfPage.getImage(mGraphView.getWidth() * 2, mGraphView.getHeight() * 2, null, true, true);
final Bitmap bitmapWithoutQualityLose = getBitmapWithoutQualityLose(bitmap, mGraphView.getWidth(), mGraphView.getHeight());
mGraphView.setPageBitmap(bitmapWithoutQualityLose);
final DisplayMetrics displayMetrics = UiUtils.OfMetrics.getDisplayMetrics(getActivity());
final int density = (int) displayMetrics.density;
float width = mPdfPage.getWidth() * density;
float height = mPdfPage.getHeight() * density;
int maxWidthToPopulate = mGraphView.getWidth();
int maxHeightToPopulate = mGraphView.getHeight();
int calculatedWidth;
int calculatedHeight;
if (width < maxWidthToPopulate && height < maxHeightToPopulate) {
calculatedWidth = (int) width;
calculatedHeight = (int) height;
} else {
final float widthRatio = width / maxWidthToPopulate;
final float heightRatio = height / maxHeightToPopulate;
if (widthRatio > heightRatio) {
calculatedHeight = (int) (height / widthRatio);
calculatedWidth = (int) (width / widthRatio);
} else {
calculatedHeight = (int) (height / heightRatio);
calculatedWidth = (int) (width / heightRatio);
}
}
Bitmap bitmap = mPdfPage.getImage(calculatedWidth, calculatedHeight, null, true, true);
bitmap = getBitmapWithoutQualityLose(bitmap, maxWidthToPopulate, maxHeightToPopulate);
mGraphView.setPageBitmap(bitmap);
mGraphView.updateImage();
} catch (Throwable e) {
Log.e(TAG, e.getMessage(), e);
}
hideProgressBar();
}
@NonNull

View File

@ -0,0 +1,100 @@
package net.sf.andpdf.pdfviewer;
/*
* Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov)
*
* This file is part of RoboSwag library.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import java.util.concurrent.atomic.AtomicInteger;
public final class UiUtils {
private UiUtils() {
}
/**
* Utilities methods related to metrics.
*/
public static class OfMetrics {
private static final int MAX_METRICS_TRIES_COUNT = 5;
/**
* Returns right metrics with non-zero height/width.
* It is common bug when metrics are calling at {@link Application#onCreate()} method and it returns metrics with zero height/width.
*
* @param context {@link Context} of metrics;
* @return {@link DisplayMetrics}.
*/
@SuppressWarnings("BusyWait")
@NonNull
public static DisplayMetrics getDisplayMetrics(@NonNull final Context context) {
DisplayMetrics result = context.getResources().getDisplayMetrics();
// it is needed to avoid bug with invalid metrics when user restore application from other application
int metricsTryNumber = 0;
while (metricsTryNumber < MAX_METRICS_TRIES_COUNT && (result.heightPixels <= 0 || result.widthPixels <= 0)) {
try {
Thread.sleep(500);
} catch (final InterruptedException ignored) {
return result;
}
result = context.getResources().getDisplayMetrics();
metricsTryNumber++;
}
return result;
}
/**
* Simply converts DP to pixels.
*
* @param context {@link Context} of metrics;
* @param sizeInDp Size in DP;
* @return Size in pixels.
*/
public static float dpToPixels(@NonNull final Context context, final float sizeInDp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDp, getDisplayMetrics(context));
}
private OfMetrics() {
}
}
}