Code modification from there( Summary of Open Source Android pdf Reader Development ) The author shared the code of Google code, and after careful reading, sorted out the main part of reading PDF documents (the use of vudroid so library). The original author should also have slightly modified the code under the vudroid project. The code is a bit messy, so I sorted out the key points, and the project did not provide the source code of vudroid so, so the original author did not provide the source code of the vudroid so library. There are only a few interfaces that you know. To get a full understanding of the vudroid library interface, you should download the official vudroid source code and compile it by yourself through ndk. (Source download is still at the end.)
import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.RectF; import android.view.Menu; import android.view.MotionEvent; import android.widget.ImageView; import android.graphics.Matrix; import android.graphics.PointF; import android.util.FloatMath; import org.vudroid.pdfdroid.codec.*; public class MainActivity extends Activity { private PdfContext pdf_conext; private ImageView view; private float screen_width = 800; private float screen_height = 1280; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view = (ImageView)findViewById(R.id.imageView1); pdf_conext = new PdfContext(); PdfDocument d = pdf_conext.openDocument("/sdcard/test.pdf"); PdfPage vuPage = d.getPage(1); RectF rf = new RectF(); rf.bottom = rf.right = (float)1.0; //The top, left, bottom and right of the parameter 3 Rect are the percentage position of the intercepted part in the page. //For example, top = 0.5, left = 0.5, right = 1.0, and 1.0 cut a quarter of the lower right corner. Bitmap bitmap = vuPage.renderBitmap((int)screen_width, (int)screen_height, rf); view.setImageBitmap(bitmap); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix(); static final int NONE = 0; static final int DRAG = 1; static final int ZOOM = 2; int mode = NONE; PointF start = new PointF(); PointF mid = new PointF(); float oldDist = 1f; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: matrix.set(view.getImageMatrix()); savedMatrix.set(matrix); start.set(event.getX(), event.getY()); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); if (oldDist > 10f) { savedMatrix.set(matrix); midPoint(mid, event); mode = ZOOM; } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = NONE; break; case MotionEvent.ACTION_MOVE: if (mode == DRAG) { matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); keepInSight(matrix); } else if (mode == ZOOM) { float newDist = spacing(event); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oldDist; matrix.postScale(scale, scale, mid.x, mid.y); keepInSight(matrix); } } break; } view.setImageMatrix(matrix); return true; } private float spacing(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return FloatMath.sqrt(x * x + y * y); } private void midPoint(PointF point, MotionEvent event) { float x = event.getX(0) + event.getX(1); float y = event.getY(0) + event.getY(1); point.set(x / 2, y / 2); } private void keepInSight(Matrix m){ float f[] = new float[9]; m.getValues(f); if(f[0]<1) f[0]=1; if(f[4]<1) f[4]=1; if(f[2]>0) f[2]=0; if(f[5]>0) f[5]=0; float l = screen_width-screen_width*f[0]; if(f[2]<l) f[2] = l; float t = screen_height-screen_height*f[0]; if(f[5]<t) f[5] = t; m.setValues(f); } }
Source download:
Reproduced in: https://my.oschina.net/gal/blog/200182