package com.fsproject.pdftest; 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; 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); } }
评论