Android gestures zoom pictures and pictures glued to your finger as you move with gestures

Source: Internet
Author: User
Tags gety

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 (; Mimageview.setontouchlistener (listener); 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.