An android gesture to zoom a picture of the tool class, and this class also implements another function: when the finger presses on the touch screen to move, the picture "sticky" moves on the finger with the finger movement and the whole movement.
The method can be used as follows: First a new instance of this class, and then the ImageView method Setontouchlistener (New Imageviewonmultitouchlistener ());
For example:
Imageviewonmultitouchlistener listener=new Imageviewonmultitouchlistener (); ImageView mimageview= (ImageView) Findviewbyid (R.id.imageview); Mimageview.setontouchlistener (listener);
Imageviewonmultitouchlistener.java All source code:
public class Imageviewonmultitouchlistener implements View.ontouchlistener {private final int NONE = 0;private final int M OVE = 1;private final int ZOOM = 2;private final int DRAG = 3;private int mode = none;private Matrix matrix = new Matrix () ;p rivate Matrix Savedmatrix = new Matrix ();p rivate PointF start = new PointF ();p rivate PointF mid = new PointF ();p rivate F Loat olddistance = 0; @Overridepublic boolean onTouch (View V, motionevent event) {ImageView view = (ImageView) v;switch (EV Ent.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:olddistance = Spacing (event), if (Olddistance > 5f) {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);} else if (mode = = ZOOM) {Float newdist = spacing (event), if (Newdist > 5f) {matrix.set (Savedmatrix); float scale = Newdist /Olddistance;matrix.postscale (scale, scale, mid.x, MID.Y);}} break;} View.setimagematrix (matrix); View.setscaletype (ImageView.ScaleType.MATRIX); view.setpadding (3, 5, 3, 5); 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);p oint.set (X/2, Y/2);}}
Android gestures zoom pictures and pictures glued to your finger as you move with gestures