MultiTouch ———— Multi-touch, telescopic picture, change picture position

Source: Internet
Author: User
Tags gety

Today's mobile phones all support multi-touch function (can be image stretching, changing position), but how do we programmers to combine the hardware to achieve this function?

Follow me to learn this function

International practice: First on demo free: http://download.csdn.net/detail/cnwutianhao/9443667

Example Picture:

I recorded with Genymotion, no real-world multi-touch display of the effect so good, we run the program on the real machine, you will experience the multi-touch function. (Note: Genymotion Multi-touch shortcut is CTRL + Drag the mouse pointer up or down.)


Specific code implementation:


Layout file Activtiy_main.xml

1<?xml version= "1.0" encoding= "Utf-8"?>2<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6tools:context= "Com.cnwuth.mutiltouch.MainActivity" >7       8<ImageView9Android:id= "@+id/iv"TenAndroid:layout_width= "Match_parent" Oneandroid:layout_height= "Match_parent" AAndroid:scaletype= "Matrix" -android:src= "@mipmap/ic_launcher"/> -        the</RelativeLayout>

Mainactivity.java

1      PackageCom.cnwuth.mutiltouch; 2       3     ImportAndroid.graphics.Matrix; 4     ImportAndroid.graphics.PointF; 5     Importandroid.support.v7.app.AppCompatActivity; 6     ImportAndroid.os.Bundle; 7     Importandroid.view.MotionEvent; 8     ImportAndroid.view.View; 9     ImportAndroid.widget.ImageView; Ten        One      Public classMainactivityextendsAppcompatactivityImplementsview.ontouchlistener{ A        -         PrivateImageView Mimageview;  -        the         //Zoom Control -         PrivateMatrix Mmatrix =NewMatrix ();  -         PrivateMatrix Savedmatrix =NewMatrix ();  -        +         //representations of different states -         Private Static Final intNONE = 0;  +         Private Static Final intDRAG = 1;  A         Private Static Final intZOOM = 2;  at         Private intmode =NONE;  -        -         //define the first pressed point, the focus of the two contact points, and the distance between the two fingers pressed: -         PrivatePointF StartPoint =NewPointF ();  -         PrivatePointF midpoint =NewPointF ();  -         Private floatOridis =1f;  in        - @Override to         protected voidonCreate (Bundle savedinstancestate) { +             Super. OnCreate (savedinstancestate);  - Setcontentview (R.layout.activity_main);  theMimageview =(ImageView) Findviewbyid (R.ID.IV);  *Mimageview.setontouchlistener ( This);  $         }  Panax Notoginseng        - @Override the          Public BooleanOnTouch (View V, motionevent event) { +ImageView view =(ImageView) v;  A             Switch(Event.getaction () &motionevent.action_mask) the             {   +                 //single Finger -                  CaseMotionevent.action_down: $ Mmatrix.set (View.getimagematrix ());  $ Savedmatrix.set (Mmatrix);  - Startpoint.set (Event.getx (), event.gety ());  -mode =DRAG;  the                      Break;  -                 //two fingersWuyi                  CaseMotionevent.action_pointer_down: theOridis =distance (event);  -                     if(Oridis >10f) Wu                     {   - Savedmatrix.set (Mmatrix);  AboutMidpoint =Middle (event);  $mode =ZOOM;  -                     }   -                      Break;  -                 //Finger Release A                  Casemotionevent.action_up: +                  Casemotionevent.action_pointer_up: themode =NONE;  -                      Break;  $                 //single finger swipe event the                  CaseMotionevent.action_move: the                     if(Mode = =DRAG) the                     {   the                         //is a finger-dragging - Mmatrix.set (Savedmatrix);  inMmatrix.posttranslate (Event.getx ()-Startpoint.x, event.gety ()-startpoint.y);  the                     }   the                     Else if(Mode = =ZOOM) About                     {   the                         //two finger swipe the                         floatNewdist =distance (event);  the                         if(Newdist >10f) +                         {   - Mmatrix.set (Savedmatrix);  the                             floatScale = newdist/Oridis; Bayi Mmatrix.postscale (scale, scale, midpoint.x, MIDPOINT.Y);  the                         }   the                     }   -                      Break;  -             }   the             //set the matrix for ImageView the View.setimagematrix (Mmatrix);  the             return true;  the         }   -        the         //calculates the distance between two touch points the         Private floatDistance (Motionevent event) { the             floatx = Event.getx (0)-Event.getx (1); 94             floaty = event.gety (0)-event.gety (1);  the             return(float) math.sqrt (x * x + y *y);  the         }   the       98         //calculates the midpoint of a two touch point About         PrivatePointF Middle (motionevent event) { -             floatx = Event.getx (0) + event.getx (1); 101             floaty = event.gety (0) + event.gety (1); 102             return NewPointF (X/2, Y/2); 103         }  104}

(Note: The environment needs to be run under Android Studio, and the SDK updates to the latest, otherwise there will be an error)


Follow me on the latest news; Sina Weibo @ Wu Tianhao Tnnowu Address: Weibo.com/cnwutianhao

MultiTouch ———— Multi-touch, telescopic picture, change picture position

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: info-contact@alibabacloud.com 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.