Android Multi-Touch interactive processing, zooming in and out of pictures

Source: Internet
Author: User
Tags gety

Multi-Touch (MultiTouch) refers to a technique that allows a computer user to control a graphical interface through multiple fingers at the same time. and multi-touch technology is a single touch, single touch device has been for many years, small size of the touch-type mobile phone, the most common in large size is the bank ATM and queuing query machine and so on.Multi-touch technology in the actual development process, the most used is the amplification function. For example, there are some picture browsers, you can use more than one finger on the screen, the image to zoom in or zoom out. For example, some browsers can also zoom in or out of a font with multi-touch. In fact, zooming in and out is just one example of the practical application of multi-touch, with multi-touch technology, to a certain extent can innovate more ways to achieve more cool human-computer interaction. theoretically, the Android system itself can handle up to 256 finger touches, depending on the support of the phone hardware. Of course, phones that support multi-touch will not support so many points, generally supporting 2 or 4 points. For developers, there is no big difference between writing multi-touch code and writing a single-touch code. This is because the Motionevent class in the Android SDK not only encapsulates a single-touch message, but also encapsulates a multi-touch message, which is almost identical for single-touch and multi-touch processing. in dealing with a single touch, we typically use Motionevent.action_down, action_up, Action_move, and then we can use a switch statement to do the processing separately. Action_down and Action_up is a single touch screen, press down and release the operation, Action_move is the finger on the screen to move the operation. In the process of handling multi-touch, we also need to use the motionevent.action_mask. Typically, switch (Event.getaction () & Motionevent.action_mask) is used to handle Action_pointer_down and ACTION_POINTER_UP events that handle multi-touch. After the code calls this "and" operation, when the second finger is pressed or released, the Action_pointer_down or Action_pointer_up event is triggered. let's take a practical example to illustrate how to implement multi-touch functionality in your code. Here we load a picture, after loading the picture, you can drag the picture through a finger, you can also use two finger swipe to achieve the image zoom reduction function. View
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "    android:layout_height=" Match_parent "Android: paddingleft= "@dimen/activity_horizontal_margin"    android:paddingright= "@dimen/activity_horizontal_margin"    android:paddingtop= "@dimen/activity_vertical_margin"    android:paddingbottom= "@dimen/activity_vertical_ Margin "tools:context=". Mainactivity "    android:id=" @+id/frame ">    <imageview        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:id= "@+id/img"        android:background= "@drawable/paoku00199"/ ></FrameLayout>

Java
private float currentdistance=0;    private float lastdistance=-1;    Private Framelayout frame;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Frame= (framelayout) Findviewbyid (r.id.frame);        Final ImageView img= (ImageView) Findviewbyid (r.id.img); Frame.setontouchlistener (New View.ontouchlistener () {@Override public boolean onTouch (View V, motio                        Nevent event) {switch (event.getaction ()) {case Motionevent.action_down://Touch Press                        LOG.I ("Motionevent", "Action_down");                Break                        } switch (Event.getaction ()) {case Motionevent.action_move://Touch move                        LOG.I ("Motionevent", "Action_move");     Framelayout.layoutparams layoutparams = (framelayout.layoutparams) img.getlayoutparams ();                   if (Event.getpointercount () ==1) {//single touch point, and drag picture Sy                            Stem.out.println (String.Format ("X:%f,y:%f", Event.getx (), Event.gety ()));                            Layoutparams.leftmargin = (int) event.getx ()-10;                            Layoutparams.topmargin = (int) event.gety ()-10;                        Img.setlayoutparams (Layoutparams);                        } System.out.println ("Number of pointer count touch points" +event.getpointercount ());//number of touch points                            Multiple touch point coordinates//gesture Zoom Out image if (Event.getpointercount () >=2) { System.out.println (String.Format ("Multi-touch point coordinates x1:%f;                            Y1:%f ", Event.getx (0), Event.getx (0));                            Float offsetx=event.getx (0)-event.getx (1);                            Float offsety=event.gety (0)-event.gety (1); Currentdistance= (float) math.sqrt (offsetx*offsetx+offsety*offSety);                            if (lastdistance<0) {lastdistance=currentdistance;                                   }else {if (currentdistance-lastdistance>5) {//5px; error                                   Framelayout.layoutparams LP = (framelayout.layoutparams) img.getlayoutparams ();                                   SYSTEM.OUT.PRINTLN ("enlarge");                                   lp.width= (int) (1.1f*img.getwidth ());                                   lp.height= (int) (1.1f*img.getheight ());                                   IMG.SETLAYOUTPARAMS (LP);  Lastdistance=currentdistance; Enlarge}else if (lastdistance-currentdistance>5) {Lastdis    Tance=currentdistance;                                   Narrow Framelayout.layoutparams LP = (framelayout.layoutparams) img.getlayoutparams (); lp.width= (int) (0.9f*img.getwidth ());                                   lp.height= (int) (0.9f*img.getheight ());                               IMG.SETLAYOUTPARAMS (LP);                }}} break;                        } switch (Event.getaction ()) {case MOTIONEVENT.ACTION_UP://Touch Bounce up                        LOG.I ("Motionevent", "action_up");                Break    Return True;//true: Need to presses follow-up events; false: No subsequent events (only one execution)}) are required; }
Attention:
Private  float currentdistance=0;    private float lastdistance=-1;
To have the onclick written out;

Android Multi-Touch interactive processing, zooming in and out of pictures

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: 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.