Android multi-touch is essentially supported by LCD drivers and programming. Currently, mobile phones that use capacitive screen touch can support multi-touch multitouch technology, provides a better user experience for webpage scaling and gesture operations. On the Android platform, events are processed using the motionevent object method. For example, when a touch is started, action_down is triggered, and action_up is triggered when action_move finally releases the finger. Of course, user-less operations may trigger the action_cancel action.
The ontouchevent () method implemented by the setontouchlistener () interface of the view is used internally for the touch operation of the general control.
The following uses imageview for Demonstration:
Main. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <framelayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <imageview Android: Id = "@ + ID/imageview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: src = "@ drawable/PIC" <br/> Android: scaletype = "matrix"> <br/> </imageview> <br/> </framelayout>
Note that the attribute of Android: scaletype must be set to matrix. Otherwise, the image size cannot be changed.
Java file:
Package COM. shao. muti; </P> <p> Import android. app. activity; <br/> Import android. graphics. matrix; <br/> Import android. graphics. pointf; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. motionevent; <br/> Import android. view. view; <br/> Import android. view. view. ontouchlistener; <br/> Import android. widget. imageview; </P> <p> public class mutitouchactivity extends activity implements ontouchlistener {<br/>/** called when the activity is first created. */<br/> Private Static final string tag = "Touch"; <br/> Private Static final int none = 0; <br/> Private Static final int drag = 1; <br/> Private Static final int zoom = 2; <br/> int mode = none; <br/> matrix = new matrix (); <br/> matrix savedmatrix = new matrix (); </P> <p> pointf start = new pointf (); <br/> pointf mid = new pointf (); <br/> float olddist = 1f; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> imageview view = (imageview) findviewbyid (R. id. imageview); <br/> View. setontouchlistener (this); <br/>}</P> <p> @ override <br/> Public Boolean ontouch (view V, motionevent event) {<br/> // todo auto-generated method stub <br/> imageview view = (imageview) V; <br/> printeventinfo (event ); // test output information <br/> switch (event. getaction () & motionevent. action_mask) <br/>{< br/> case motionevent. action_down: <br/> savedmatrix. set (matrix); <br/> // set the initial point position <br/> Start. set (event. getx (), event. gety (); <br/> log. D (TAG, "mode = drag"); <br/> mode = drag; <br/> break; <br/> case motionevent. action_pointer_partition down: <br/> olddist = spacing (event); <br/> log. D (TAG, "loddist =" + olddist); <br/> If (olddist> 10f) {<br/> savedmatrix. set (matrix); <br/> midpoint (MID, event); <br/> mode = zoom; <br/> log. D (TAG, "mode = zoom"); </P> <p >}< br/> break; <br/> case motionevent. action_up: <br/> case motionevent. action_pointer_reply up: <br/> mode = none; <br/> log. D (TAG, "mode = none"); <br/> break; <br/> case motionevent. action_move: <br/> If (mode = drag) {<br/> matrix. set (savedmatrix); <br/> matrix. posttranslate (event. getx ()-start. x, event. gety ()-start. y); <br/>}< br/> else if (mode = zoom) {<br/> float newdist = spacing (event); <br/> log. D (TAG, "newdist =" + newdist); <br/> If (newdist> 10f) {<br/> matrix. set (savedmatrix); <br/> float scale = newdist/olddist; <br/> matrix. postscale (scale, scale, mid. x, mid. y); <br/>}< br/> break; <br/>}< br/> View. setimagematrix (matrix); <br/> return true; </P> <p >}< br/> private void printeventinfo (motionevent event) {<br/> int num = 0; <br/> system. out. println ("Touch action ---->" + event. getaction (); // obtain the touch action such as action_down <br/> system. out. println ("Number of touch points --->" + event. getpointercount (); // obtain the number of touch points. For example, if 2 is used, two fingers may simultaneously press the screen. <br/> num = event. getpointercount (); <br/> for (INT I = 0; I <num; I ++) {<br/> system. out. println ("index -->" + event. getpointerid (I); // For the details of each touch point, we can execute the getpointerid method in a loop to obtain the index <br/> system. out. println ("X position of the I touch point -->" + event. getx (I); // obtain the X position of the I-touch point. <br/> system. out. println ("Y position of the I touch point -->" + event. gety (I); // obtain the Y position of the I-point touch. <br/> system. out. println ("Finger Pressure -->" + event. getpressure (I); // LCD can sense the user's finger pressure. Of course, the specific level is determined by the driver and physical hardware <br/>}< br/> system. out. println ("Start Time" + event. getdowntime (); // press start time <br/> system. out. println ("event End Time" + event. geteventtime (); // event end time <br/> system. out. println ("total time spent on pressing" + (event. geteventtime ()-event. getdowntime ())); // It Takes a total of time to press </P> <p >}< br/>/** determine the space between the first two fingers */<br/> private float spacing (motionevent event) {<br/> float x = event. getx (0)-event. gety (1); <br/> float y = event. gety (0)-event. gety (1); <br/> return (float) math. SQRT (x * x + y * y ); <br/>}< br/>/** calculate the mid point of the first two fingers */<br/> private void midpoint (pointf point, motionevent event) {<br/> float x = event. getx (0) + event. getx (1); <br/> float y = event. gety (0) + event. gety (1); <br/> point. set (X/2, Y/2); <br/>}< br/>}