refactor pdfView

This commit is contained in:
weiyixiong 2015-11-17 13:05:37 +08:00
parent e965eaf977
commit 2848d4cb46
30 changed files with 967 additions and 209 deletions

File diff suppressed because it is too large Load Diff

View File

@ -48,6 +48,7 @@ import java.nio.channels.FileChannel;
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;
/**
@ -195,6 +196,7 @@ public abstract class PdfViewerActivity extends Activity {
private void setContent(String password) {
try {
parsePDF(pdffilename, password);
pdfView.setmPdfFile(mPdfFile);
setContentView(mGraphView);
startRenderThread(mPage, mZoom);
} catch (PDFAuthenticationFailureException e) {
@ -430,6 +432,9 @@ public abstract class PdfViewerActivity extends Activity {
return null;
}
//TODO
PdfView pdfView;
private class GraphView extends FullScrollView {
//private String mText;
//private long fileMillis;
@ -455,9 +460,10 @@ public abstract class PdfViewerActivity extends Activity {
//setContentView(R.layout.graphics_view);
// layout params
LinearLayout.LayoutParams lpWrap1 =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lpWrap10 =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 10);
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// vertical layout
LinearLayout vl = new LinearLayout(context);
@ -467,6 +473,8 @@ public abstract 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
@ -476,17 +484,21 @@ public abstract class PdfViewerActivity extends Activity {
setPageBitmap(null);
updateImage();
mImageView.setLayoutParams(lpWrap1);
mImageView.setPadding(5, 5, 5, 5);
// mImageView.setPadding(5, 5, 5, 5);
vl.addView(mImageView);
//TODO
vl.addView(pdfView);
pdfView.setLayoutParams(lpWrap1);
/*mImageView = (ImageView) findViewById(R.id.pdf_image);
if (mImageView == null) {
Log.i(TAG, "mImageView is null!!!!!!");
}
setPageBitmap(null);
updateImage();*/
/*
navigationPanel = new ViewStub(PdfViewerActivity.this, R.layout.navigation_overlay);
navigationPanel = new ViewStub(PdfViewerActivity.this, R.layout.navigation_overlay);
final ImageButton previous = (ImageButton)navigationPanel.findViewById(R.id.navigation_previous);
previous.setBackgroundDrawable(null);
previous.setOnClickListener(new OnClickListener() {
@ -527,7 +539,7 @@ public abstract class PdfViewerActivity extends Activity {
//addNavButtons(vl);
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 100));
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setBackgroundColor(Color.LTGRAY);
setHorizontalScrollBarEnabled(true);
setHorizontalFadingEdgeEnabled(true);
@ -538,12 +550,12 @@ public abstract class PdfViewerActivity extends Activity {
private void addNavButtons(ViewGroup vg) {
addSpace(vg, 6, 6);
// addSpace(vg, 6, 6);
LinearLayout.LayoutParams lpChild1 =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lpWrap10 =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 10);
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Context context = vg.getContext();
LinearLayout hl = new LinearLayout(context);
@ -578,7 +590,7 @@ public abstract class PdfViewerActivity extends Activity {
});
hl.addView(bZoomIn);
addSpace(hl, 6, 6);
// addSpace(hl, 6, 6);
// prev button
ImageButton bPrev = new ImageButton(context);
@ -620,11 +632,11 @@ public abstract class PdfViewerActivity extends Activity {
});
hl.addView(bNext);
addSpace(hl, 20, 20);
// addSpace(hl, 20, 20);
// exit button
/*
Button bExit=new Button(context);
/*
Button bExit=new Button(context);
bExit.setLayoutParams(lpChild1);
bExit.setText("Back");
bExit.setWidth(60);
@ -668,9 +680,9 @@ public abstract class PdfViewerActivity extends Activity {
uiHandler.post(new Runnable() {
public void run() {
mImageView.setImageBitmap(mBi);
/*if (progress != null)
progress.dismiss();*/
progress.dismiss();*/
}
});
}
@ -679,8 +691,8 @@ public abstract class PdfViewerActivity extends Activity {
if (bi != null) {
mBi = bi;
} else {
/*
mBi = Bitmap.createBitmap(100, 100, Config.RGB_565);
/*
mBi = Bitmap.createBitmap(100, 100, Config.RGB_565);
Canvas can = new Canvas(mBi);
can.drawColor(Color.RED);
@ -707,6 +719,7 @@ public abstract class PdfViewerActivity extends Activity {
}
private void showPage(int page, float zoom) throws Exception {
pdfView.showPage(page, zoom);
//long startTime = System.currentTimeMillis();
//long middleTime = startTime;
try {
@ -787,13 +800,12 @@ public abstract class PdfViewerActivity extends Activity {
} else {
mPdfFile = new PDFFile(bb, new PDFPassword(password));
}
mGraphView.showText("Anzahl Seiten:" + mPdfFile.getNumPages());
}
/*private byte[] readBytes(File srcFile) throws IOException {
long fileLength = srcFile.length();
long fileLength = srcFile.length();
int len = (int)fileLength;
byte[] result = new byte[len];
FileInputStream fis = new FileInputStream(srcFile);
@ -846,7 +858,7 @@ public abstract class PdfViewerActivity extends Activity {
}
/*private void postHideNavigation() {
// Start a time to hide the panel after 3 seconds
// Start a time to hide the panel after 3 seconds
closeNavigationHandler.removeCallbacks(closeNavigationThread);
closeNavigationHandler.postDelayed(closeNavigationThread, 3000);
}*/

View File

@ -0,0 +1,239 @@
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.Log;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.decrypt.PDFAuthenticationFailureException;
import com.sun.pdfview.decrypt.PDFPassword;
import net.sf.andpdf.nio.ByteBuffer;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
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;
private ImageView mImageView;
private Handler uiHandler;
ImageButton bZoomOut;
ImageButton bZoomIn;
private PDFFile mPdfFile;
private PDFPage mPdfPage;
private Thread backgroundThread;
private int mPage;
private float mZoom;
public PdfView(Context context) {
super(context);
uiHandler = new Handler();
LayoutParams matchLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mImageView = new ImageView(context);
setPageBitmap(null);
updateImage();
mImageView.setLayoutParams(matchLp);
addView(mImageView);
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setBackgroundColor(Color.LTGRAY);
setHorizontalScrollBarEnabled(true);
setHorizontalFadingEdgeEnabled(true);
setVerticalScrollBarEnabled(true);
setVerticalFadingEdgeEnabled(true);
}
public PDFFile getmPdfFile() {
return mPdfFile;
}
public void setmPdfFile(PDFFile mPdfFile) {
this.mPdfFile = mPdfFile;
}
public void showPage(int page, float zoom) throws Exception {
try {
// free memory from previous page
setPageBitmap(null);
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();
RectF clip = null;
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.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;
if (mZoom >= MAX_ZOOM) {
Log.d(TAG, "Disabling zoom in button");
bZoomIn.setEnabled(false);
} else {
bZoomIn.setEnabled(true);
}
bZoomOut.setEnabled(true);
startRenderThread(mPage, mZoom);
}
}
}
private void zoomOut() {
if (mPdfFile != null) {
if (mZoom > MIN_ZOOM) {
mZoom /= ZOOM_INCREMENT;
if (mZoom < MIN_ZOOM) mZoom = MIN_ZOOM;
if (mZoom <= MIN_ZOOM) {
Log.d(TAG, "Disabling zoom out button");
bZoomOut.setEnabled(false);
} else {
bZoomOut.setEnabled(true);
}
bZoomIn.setEnabled(true);
startRenderThread(mPage, mZoom);
}
}
}
private void nextPage() {
if (mPdfFile != null) {
if (mPage < mPdfFile.getNumPages()) {
mPage += 1;
bZoomOut.setEnabled(true);
bZoomIn.setEnabled(true);
startRenderThread(mPage, mZoom);
}
}
}
private void prevPage() {
if (mPdfFile != null) {
if (mPage > 1) {
mPage -= 1;
bZoomOut.setEnabled(true);
bZoomIn.setEnabled(true);
startRenderThread(mPage, mZoom);
}
}
}
private void gotoPage() {
if (mPdfFile != null) {
// showDialog(DIALOG_PAGENUM);
}
}
private synchronized void startRenderThread(final int page, final float zoom) {
if (backgroundThread != null) return;
backgroundThread = new Thread(new Runnable() {
public void run() {
try {
if (mPdfFile != null) {
showPage(page, zoom);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
backgroundThread = null;
}
});
updateImageStatus();
backgroundThread.start();
}
private void updateImageStatus() {
if (backgroundThread == null) {
return;
}
postDelayed(new Runnable() {
public void run() {
updateImageStatus();
}
}, 1000);
}
private 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();
}
}

View File

@ -5,7 +5,7 @@
jar="/Users/winney/git/Android-Pdf-Viewer-Library/pdfviewsample/build/intermediates/exploded-aar/Android-Pdf-Viewer-Library/PdfView/unspecified/jars/classes.jar"
jumboMode="false"
revision="23.0.2"
sha1="37191b3bdc528f4e4708f4a7e380629a951b272d">
sha1="8cad678d9043f1f93f2c277b3890e0b109ac9b24">
<dex dex="/Users/winney/git/Android-Pdf-Viewer-Library/pdfviewsample/build/intermediates/pre-dexed/debug/classes-dbc371ee905b36ce5cd278976d41c35b063e55b0.jar" />
</item>