bug fix
This commit is contained in:
parent
07e7c3a208
commit
3570bd111d
|
|
@ -39,10 +39,8 @@ import com.sun.pdfview.font.PDFFont;
|
|||
|
||||
import net.sf.andpdf.nio.ByteBuffer;
|
||||
import net.sf.andpdf.pdfviewer.gui.FullScrollView;
|
||||
import net.sf.andpdf.pdfviewer.gui.PdfView;
|
||||
import net.sf.andpdf.refs.HardReference;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
|
|
@ -52,8 +50,6 @@ import java.io.IOException;
|
|||
*/
|
||||
public class PdfViewerActivity extends Activity {
|
||||
|
||||
public static final String BUNDLE_KEY = "BUNDLE_KEY";
|
||||
|
||||
private static final int STARTPAGE = 1;
|
||||
private static final float STARTZOOM = 1.0f;
|
||||
|
||||
|
|
@ -87,7 +83,6 @@ public class PdfViewerActivity extends Activity {
|
|||
public static byte[] byteArray;
|
||||
private int mPage;
|
||||
private float mZoom;
|
||||
private File mTmpFile;
|
||||
private ProgressDialog progress;
|
||||
|
||||
private PDFPage mPdfPage;
|
||||
|
|
@ -112,7 +107,6 @@ public class PdfViewerActivity extends Activity {
|
|||
mPage = inst.mPage;
|
||||
mPdfFile = inst.mPdfFile;
|
||||
mPdfPage = inst.mPdfPage;
|
||||
mTmpFile = inst.mTmpFile;
|
||||
mZoom = inst.mZoom;
|
||||
backgroundThread = inst.backgroundThread;
|
||||
}
|
||||
|
|
@ -133,8 +127,8 @@ public class PdfViewerActivity extends Activity {
|
|||
mGraphView = new GraphView(this);
|
||||
mGraphView.mBi = mOldGraphView.mBi;
|
||||
mOldGraphView = null;
|
||||
mGraphView.mImageView.setImageBitmap(mGraphView.mBi);
|
||||
// mGraphView.updateTexts();
|
||||
mGraphView.pdfView.setImageBitmap(mGraphView.mBi);
|
||||
mGraphView.updateTexts();
|
||||
setContentView(mGraphView);
|
||||
} else {
|
||||
mGraphView = new GraphView(this);
|
||||
|
|
@ -157,7 +151,6 @@ public class PdfViewerActivity extends Activity {
|
|||
private void setContent(String password) {
|
||||
try {
|
||||
openFile(byteArray, password);
|
||||
pdfView.setmPdfFile(mPdfFile);
|
||||
setContentView(mGraphView);
|
||||
startRenderThread(mPage, mZoom);
|
||||
} catch (PDFAuthenticationFailureException e) {
|
||||
|
|
@ -201,8 +194,10 @@ public class PdfViewerActivity extends Activity {
|
|||
|
||||
private void updateImageStatus() {
|
||||
if (backgroundThread == null) {
|
||||
mGraphView.updateUi();
|
||||
return;
|
||||
}
|
||||
mGraphView.updateUi();
|
||||
mGraphView.postDelayed(new Runnable() {
|
||||
public void run() {
|
||||
updateImageStatus();
|
||||
|
|
@ -382,13 +377,12 @@ public class PdfViewerActivity extends Activity {
|
|||
return null;
|
||||
}
|
||||
|
||||
//TODO
|
||||
PdfView pdfView;
|
||||
|
||||
private class GraphView extends FullScrollView {
|
||||
public Bitmap mBi;
|
||||
public ImageView mImageView;
|
||||
public ImageView pdfView;
|
||||
public Button mBtPage;
|
||||
private Button mBtPage2;
|
||||
|
||||
ImageButton bZoomOut;
|
||||
ImageButton bZoomIn;
|
||||
|
|
@ -396,13 +390,11 @@ public class PdfViewerActivity extends Activity {
|
|||
public GraphView(Context context) {
|
||||
super(context);
|
||||
|
||||
LinearLayout.LayoutParams lpWrap1 =
|
||||
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
||||
LinearLayout.LayoutParams lpWrap10 =
|
||||
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
|
||||
// LinearLayout.LayoutParams matchLp =
|
||||
// new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||
|
||||
LinearLayout.LayoutParams matchLp =
|
||||
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||
LinearLayout.LayoutParams lpWrap1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
|
||||
LinearLayout.LayoutParams lpWrap10 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 10);
|
||||
|
||||
LinearLayout vl = new LinearLayout(context);
|
||||
vl.setLayoutParams(lpWrap10);
|
||||
|
|
@ -411,20 +403,19 @@ public class PdfViewerActivity extends Activity {
|
|||
if (mOldGraphView == null) {
|
||||
progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page", true, true);
|
||||
}
|
||||
//TODO
|
||||
pdfView = new PdfView(PdfViewerActivity.this);
|
||||
|
||||
addNavButtons(vl);
|
||||
// remember page button for updates
|
||||
mBtPage2 = mBtPage;
|
||||
|
||||
mImageView = new ImageView(context);
|
||||
pdfView = new ImageView(context);
|
||||
setPageBitmap(null);
|
||||
updateImage();
|
||||
mImageView.setLayoutParams(lpWrap1);
|
||||
vl.addView(mImageView);
|
||||
pdfView.setLayoutParams(lpWrap1);
|
||||
pdfView.setPadding(5, 5, 5, 5);
|
||||
vl.addView(pdfView);
|
||||
pdfView.setLayoutParams(matchLp);
|
||||
|
||||
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
||||
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 100));
|
||||
setBackgroundColor(Color.LTGRAY);
|
||||
setHorizontalScrollBarEnabled(true);
|
||||
setHorizontalFadingEdgeEnabled(true);
|
||||
|
|
@ -435,10 +426,8 @@ public class PdfViewerActivity extends Activity {
|
|||
|
||||
private void addNavButtons(ViewGroup vg) {
|
||||
|
||||
LinearLayout.LayoutParams lpChild1 =
|
||||
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
||||
LinearLayout.LayoutParams lpWrap10 =
|
||||
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
||||
LinearLayout.LayoutParams lpChild1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
|
||||
LinearLayout.LayoutParams lpWrap10 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 10);
|
||||
|
||||
Context context = vg.getContext();
|
||||
LinearLayout hl = new LinearLayout(context);
|
||||
|
|
@ -479,7 +468,6 @@ public class PdfViewerActivity extends Activity {
|
|||
bPrev.setOnClickListener(new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
prevPage();
|
||||
updatePageNumber();
|
||||
}
|
||||
});
|
||||
hl.addView(bPrev);
|
||||
|
|
@ -491,7 +479,6 @@ public class PdfViewerActivity extends Activity {
|
|||
mBtPage.setOnClickListener(new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
gotoPage();
|
||||
updatePageNumber();
|
||||
}
|
||||
});
|
||||
hl.addView(mBtPage);
|
||||
|
|
@ -504,7 +491,6 @@ public class PdfViewerActivity extends Activity {
|
|||
bNext.setOnClickListener(new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
nextPage();
|
||||
updatePageNumber();
|
||||
}
|
||||
});
|
||||
hl.addView(bNext);
|
||||
|
|
@ -524,7 +510,15 @@ public class PdfViewerActivity extends Activity {
|
|||
private void updateImage() {
|
||||
uiHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
mImageView.setImageBitmap(mBi);
|
||||
pdfView.setImageBitmap(mBi);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updateUi() {
|
||||
uiHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
updateTexts();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -535,24 +529,24 @@ public class PdfViewerActivity extends Activity {
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updatePageNumber() {
|
||||
Runnable updatePageNumber = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String maxPage = ((mPdfFile == null) ? "0" : Integer.toString(mPdfFile.getNumPages()));
|
||||
mGraphView.mBtPage.setText(mPage + "/" + maxPage);
|
||||
protected void updateTexts() {
|
||||
if (mPdfPage != null) {
|
||||
if (mBtPage != null)
|
||||
mBtPage.setText(mPdfPage.getPageNumber() + "/" + mPdfFile.getNumPages());
|
||||
if (mBtPage2 != null)
|
||||
mBtPage2.setText(mPdfPage.getPageNumber() + "/" + mPdfFile.getNumPages());
|
||||
}
|
||||
};
|
||||
uiHandler.post(updatePageNumber);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void showPage(int page, float zoom) throws Exception {
|
||||
pdfView.showPage(page, zoom);
|
||||
updatePageNumber();
|
||||
try {
|
||||
|
||||
// free memory from previous page
|
||||
mGraphView.setPageBitmap(null);
|
||||
mGraphView.updateImage();
|
||||
|
||||
// Only load the page if it's a different page (i.e. not just changing the zoom level)
|
||||
if (mPdfPage == null || mPdfPage.getPageNumber() != page) {
|
||||
mPdfPage = mPdfFile.getPage(page, true);
|
||||
|
|
@ -599,10 +593,6 @@ public class PdfViewerActivity extends Activity {
|
|||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (mTmpFile != null) {
|
||||
mTmpFile.delete();
|
||||
mTmpFile = null;
|
||||
}
|
||||
byteArray = null;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,293 +0,0 @@
|
|||
package net.sf.andpdf.pdfviewer.gui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.RectF;
|
||||
import android.os.Handler;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.Toast;
|
||||
import com.polites.android.Direction;
|
||||
import com.polites.android.GestureImageView;
|
||||
import com.polites.android.GestureImageViewTouchListener;
|
||||
import com.sun.pdfview.PDFFile;
|
||||
import com.sun.pdfview.PDFImage;
|
||||
import com.sun.pdfview.PDFPage;
|
||||
import com.sun.pdfview.PDFPaint;
|
||||
import com.sun.pdfview.decrypt.PDFAuthenticationFailureException;
|
||||
import com.sun.pdfview.decrypt.PDFPassword;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.channels.FileChannel;
|
||||
import net.sf.andpdf.nio.ByteBuffer;
|
||||
import net.sf.andpdf.refs.HardReference;
|
||||
|
||||
public class PdfView extends FullScrollView {
|
||||
|
||||
private static final int STARTPAGE = 1;
|
||||
private static final float STARTZOOM = 1.0f;
|
||||
|
||||
private static final float MIN_ZOOM = 0.25f;
|
||||
private static final float MAX_ZOOM = 3.0f;
|
||||
private static final float ZOOM_INCREMENT = 1.5f;
|
||||
|
||||
private Bitmap mBi;
|
||||
public GestureImageView mImageView;
|
||||
private Handler uiHandler;
|
||||
private PDFFile mPdfFile;
|
||||
private PDFPage mPdfPage;
|
||||
private Thread backgroundThread;
|
||||
private int mPage;
|
||||
private float mZoom;
|
||||
|
||||
private boolean drawing = false;
|
||||
|
||||
public PdfView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public PdfView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, android.R.attr.scrollViewStyle);
|
||||
}
|
||||
|
||||
public PdfView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
PDFImage.sShowImages = true;
|
||||
PDFPaint.s_doAntiAlias = true;
|
||||
HardReference.sKeepCaches = true;
|
||||
uiHandler = new Handler();
|
||||
LayoutParams matchLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
||||
mImageView = new GestureImageView(context);
|
||||
mImageView.setLayoutParams(matchLp);
|
||||
addView(mImageView);
|
||||
setLayoutParams(matchLp);
|
||||
setBackgroundColor(Color.LTGRAY);
|
||||
setHorizontalScrollBarEnabled(true);
|
||||
setHorizontalFadingEdgeEnabled(true);
|
||||
setVerticalScrollBarEnabled(true);
|
||||
setVerticalFadingEdgeEnabled(true);
|
||||
mZoom = STARTZOOM;
|
||||
mImageView.setOnClickListener(new OnClickListener() {
|
||||
@Override public void onClick(View v) {
|
||||
nextPage();
|
||||
}
|
||||
});
|
||||
mImageView.setOnReachBoundListener(new GestureImageViewTouchListener.OnReachBoundListener() {
|
||||
@Override public void onReach(View view, int direction) {
|
||||
if (drawing) {
|
||||
return;
|
||||
}
|
||||
switch (direction) {
|
||||
case Direction.LEFT:
|
||||
prevPage();
|
||||
break;
|
||||
case Direction.RIGHT:
|
||||
nextPage();
|
||||
break;
|
||||
case Direction.TOP:
|
||||
break;
|
||||
case Direction.BOTTOM:
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PDFFile getmPdfFile() {
|
||||
return mPdfFile;
|
||||
}
|
||||
|
||||
public void setmPdfFile(PDFFile mPdfFile) {
|
||||
this.mPdfFile = mPdfFile;
|
||||
}
|
||||
|
||||
private int getDeviceWidth() {
|
||||
DisplayMetrics metric = new DisplayMetrics();
|
||||
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
|
||||
wm.getDefaultDisplay().getMetrics(metric);
|
||||
return metric.widthPixels; // 屏幕宽度(像素)
|
||||
}
|
||||
|
||||
private int getDeviceHeight() {
|
||||
DisplayMetrics metric = new DisplayMetrics();
|
||||
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
|
||||
wm.getDefaultDisplay().getMetrics(metric);
|
||||
return metric.heightPixels; // 屏幕高度(像素)
|
||||
}
|
||||
|
||||
public void showPage(int page, float zoom) throws Exception {
|
||||
try {
|
||||
// free memory from previous page
|
||||
updateImage();
|
||||
// Only load the page if it's a different page (i.e. not just changing the zoom level)
|
||||
if (mPdfPage == null || mPdfPage.getPageNumber() != page) {
|
||||
mPdfPage = mPdfFile.getPage(page, true);
|
||||
}
|
||||
float width = mPdfPage.getWidth();
|
||||
float height = mPdfPage.getHeight();
|
||||
if (getLayoutParams().height == ViewGroup.LayoutParams.MATCH_PARENT) {
|
||||
height *= getDeviceWidth() / width;
|
||||
}
|
||||
if (getLayoutParams().width == LayoutParams.MATCH_PARENT) {
|
||||
width = getDeviceWidth();
|
||||
}
|
||||
RectF clip = null;
|
||||
final Bitmap bi = mPdfPage.getImage((int) (width * zoom), (int) (height * zoom), clip, true, true);
|
||||
setPageBitmap(bi);
|
||||
updateImage();
|
||||
} catch (Throwable e) {
|
||||
Log.e(TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateImage() {
|
||||
uiHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
mImageView.reset();
|
||||
mImageView.setImageBitmap(mBi);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setPageBitmap(Bitmap bi) {
|
||||
if (bi != null) {
|
||||
mBi = bi;
|
||||
}
|
||||
}
|
||||
|
||||
private void zoomIn() {
|
||||
if (mPdfFile != null) {
|
||||
if (mZoom < MAX_ZOOM) {
|
||||
mZoom *= ZOOM_INCREMENT;
|
||||
if (mZoom > MAX_ZOOM) mZoom = MAX_ZOOM;
|
||||
startRenderThread(mPage, mZoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void zoomOut() {
|
||||
if (mPdfFile != null) {
|
||||
if (mZoom > MIN_ZOOM) {
|
||||
mZoom /= ZOOM_INCREMENT;
|
||||
if (mZoom < MIN_ZOOM) mZoom = MIN_ZOOM;
|
||||
startRenderThread(mPage, mZoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void nextPage() {
|
||||
if (mPdfFile != null) {
|
||||
if (mPage < mPdfFile.getNumPages()) {
|
||||
mPage += 1;
|
||||
startRenderThread(mPage, mZoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void prevPage() {
|
||||
if (mPdfFile != null) {
|
||||
if (mPage > 1) {
|
||||
mPage -= 1;
|
||||
startRenderThread(mPage, mZoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void gotoPage() {
|
||||
if (mPdfFile != null) {
|
||||
// showDialog(DIALOG_PAGENUM);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void startRenderThread(final int page, final float zoom) {
|
||||
if (backgroundThread != null) return;
|
||||
backgroundThread = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
drawing = true;
|
||||
try {
|
||||
if (mPdfFile != null) {
|
||||
showPage(page, zoom);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, e.getMessage(), e);
|
||||
}
|
||||
drawing = false;
|
||||
backgroundThread = null;
|
||||
}
|
||||
});
|
||||
updateImageStatus();
|
||||
backgroundThread.start();
|
||||
}
|
||||
|
||||
private void updateImageStatus() {
|
||||
if (backgroundThread == null) {
|
||||
return;
|
||||
}
|
||||
postDelayed(new Runnable() {
|
||||
public void run() {
|
||||
updateImageStatus();
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
public void parsePDF(File f, String password) throws PDFAuthenticationFailureException {
|
||||
try {
|
||||
long len = f.length();
|
||||
if (len == 0) {
|
||||
toastMessage("file '" + f.getName() + "' not found");
|
||||
} else {
|
||||
toastMessage("file '" + f.getName() + "' has " + len + " bytes");
|
||||
openFile(f, password);
|
||||
}
|
||||
} catch (PDFAuthenticationFailureException e) {
|
||||
throw e;
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
toastMessage("Exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void parsePDF(String filename, String password) throws PDFAuthenticationFailureException {
|
||||
try {
|
||||
File f = new File(filename);
|
||||
long len = f.length();
|
||||
if (len == 0) {
|
||||
toastMessage("file '" + filename + "' not found");
|
||||
} else {
|
||||
toastMessage("file '" + filename + "' has " + len + " bytes");
|
||||
openFile(f, password);
|
||||
}
|
||||
} catch (PDFAuthenticationFailureException e) {
|
||||
throw e;
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
toastMessage("Exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void openFile(File file, String password) throws IOException {
|
||||
// first open the file for random access
|
||||
RandomAccessFile raf = new RandomAccessFile(file, "r");
|
||||
// extract a file channel
|
||||
FileChannel channel = raf.getChannel();
|
||||
// now memory-map a byte-buffer
|
||||
ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()));
|
||||
// create a PDFFile from the data
|
||||
if (password == null) {
|
||||
mPdfFile = new PDFFile(bb);
|
||||
} else {
|
||||
mPdfFile = new PDFFile(bb, new PDFPassword(password));
|
||||
}
|
||||
toastMessage("Anzahl Seiten:" + mPdfFile.getNumPages());
|
||||
}
|
||||
|
||||
public void toastMessage(String msg) {
|
||||
Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue