有哪为同学做过pdf的界面层,像ezpdfreader或者foxitreader那样的界面层。主要是文档图片的放大拖拽
------解决方案--------------------
- Java code
import android.content.Context;import android.graphics.Matrix;import android.graphics.PointF;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.util.FloatMath;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;public class DragImageView extends ImageView implements OnTouchListener { // To get an GestureDetector will be used to get DoubleClickEvent GestureDetector gd; // These matrices will be used to move and zoom image Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix(); // We can be in one of these 3 states static final int NONE = 0; static final int DRAG = 1; static final int ZOOM = 2; int mode = NONE; // Dragable && Moveable boolean Moveable = false; boolean Dragable = false; boolean Zoomable = false; // Remember some things for zooming PointF start = new PointF(); PointF end = new PointF(); PointF mid = new PointF(); float oldDist = 1f; // size of drawable float drawable_width; float drawable_height; // the point in the left and top on the screen float x_point = 0.0f; float y_point = 0.0f; // the changed size of x and y float x_change = 0.0f; float y_change = 0.0f; // the size of imageview float imageview_width; float imageview_height; public DragImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public DragImageView(Context context, AttributeSet attrs) { super(context, attrs); this.setOnTouchListener(this); gd = new GestureDetector(new mGesture()); setScaleType(ScaleType.MATRIX); drawable_height = this.getDrawable().getIntrinsicHeight(); drawable_width = this.getDrawable().getIntrinsicWidth();// Log.i("", "drawable_width=" + drawable_width);// Log.i("", "drawable_height=" + drawable_height); } public DragImageView(Context context) { super(context); } @Override public boolean onTouch(View v, MotionEvent event) { // To deal GestureEvent gd.onTouchEvent(event); imageview_width = v.getWidth(); imageview_height = v.getHeight(); // Handle touch events here... switch (event.getAction() & MotionEvent.ACTION_MASK)/* 如果想监听多触点事件必须与Action_MASK相与 */{ case MotionEvent.ACTION_DOWN: start.set(event.getX(), event.getY()); matrix.set(this.getImageMatrix()); savedMatrix.set(matrix); 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: if (Dragable) {/* 用来区分是单击还是拖动 */ x_point += (end.x - start.x); y_point += (end.y - start.y); Dragable = false; decideAnimation(x_point, y_point); } mode = NONE; break; case MotionEvent.ACTION_POINTER_UP: mode = NONE; decideAnimation(x_point, y_point); break; case MotionEvent.ACTION_MOVE: if (Moveable) { if (mode == DRAG) { // ... matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); end.set(event.getX(), event.getY()); Dragable = true; } else if (mode == ZOOM) { Zoomable = true; float newDist = spacing(event); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oldDist; matrix.postScale(scale, scale, mid.x, mid.y); // get the current size of drawable after zoomed if (scale > 1) {// When Enlarge drawable_width = drawable_width * scale + drawable_width; drawable_height = drawable_height * scale + drawable_height; } else { // When Narrow drawable_height = drawable_height - drawable_width * scale; drawable_height = drawable_height - drawable_width * scale; } } } } break; } this.setImageMatrix(matrix); return true; } // 两点间的距离 /** * @param event * @return float the distance of two point * */ 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); } // 两点的中心坐标 /** * @param PointF * to record one point * @param event * */ 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); } /** * the class extends GestureDetector to deal double tap * */ private class mGesture extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDoubleTap(MotionEvent e) { Moveable = true; return super.onDoubleTap(e); } } // 判断有没有越界 /** * @param x2 * @param y2 * @Descript the point in top and left * */ public void decideAnimation(float x2, float y2) { float top = imageview_height - drawable_height; float left = imageview_width - drawable_width;// Log.i("", "imageview_height=" + imageview_height + " imageview_width="// + imageview_width);// Log.i("", "drawable_height=" + drawable_height + " drawable_width="// + drawable_width);// Log.i("", "x2=" + x2 + "y2=" + y2);// Log.i("", "left=" + left + "top=" + top); if (drawable_width > imageview_width) { if (x2 < left) { x_point = left; } if (x2 > 0) { x_point = 0; } } if (drawable_height > imageview_height) { if (y2 < top) { y_point = top; } if (y2 > 0) { y_point = 0; } } if (drawable_width < imageview_width) { if (x2 > left) { x_point = left; } if (x2 < 0) { x_point = 0; } } if (drawable_height < imageview_height) { if (y2 > top) { y_point = top; } if (y2 < 0) { y_point = 0; } } // 发送动画请求 Message msg = new Message(); Bundle b = new Bundle(); b.putFloat("x_point", x_point); b.putFloat("y_point", y_point); msg.setData(b); handler.sendMessageAtTime(msg, 100); } // 实现动画效果 Handler handler = new Handler() { float temp_x; float temp_y; @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle b = msg.getData(); temp_x = b.getFloat("x_point"); temp_y = b.getFloat("y_point");// Log.i("", "temp_x: " + temp_x + "temp_y: " + temp_y); matrix.setTranslate(temp_x, temp_y); setImageMatrix(matrix); } };}