1Import android. app. activity; 2 Import android. OS. bundle; 3 Import android. view. motionevent; 4 Import android. view. view; 5 import android. widget. framelayout; 6 Import android. widget. framelayout. layoutparams; 7 Import android. widget. imageview; 8 9 public class mainactivity extends activity {10 11 private imageview IV; 12 private framelayout root; 13 14 @ override15 protected void oncreate (bundle savedin Stancestate) {16 super. oncreate (savedinstancestate); 17 setcontentview (R. layout. activity_main); 18 19 root = (framelayout) findviewbyid (R. id. container); 20 IV = (imageview) findviewbyid (R. id. IV); 21 22 root. setontouchlistener (new view. ontouchlistener () {23 24 float currentdistance; 25 float lastdistance =-1; // record the value recorded at most 26 27 @ override28 public Boolean ontouch (view V, motionevent event) {29 30 switc H (event. getaction () {31 case motionevent. action_down: // press 32 system. out. println ("action down"); 33 break; 34 case motionevent. action_move: // touch to move 35 // system. out. println ("action move"); 36 system. out. println ("Number of touch points:" + event. getpointercount (); 37 38 If (event. getpointercount ()> = 2) {// determine whether the value is greater than or equal to 39 40 float offsetx = event. getx (0)-event. getx (1); 41 float offsety = event. gety (0)-event. gety (1); 42 // calculate the distance between two points 43 currentdistance = (float) math. SQRT (offsetx * offsetx44 + offsety * offsety); 45 46 If (lastdistance <0) {// Initial Value 47 lastdistance = currentdistance; 48} else {49 // The current distance is greater than the previous distance by 5 pixels, which can be recognized as amplification. 50 if (currentdistance-lastdistance> 5) {51 system. out. println ("zoom in"); 52 53 framelayout. layoutparams Lp = (layoutparams) iv54. getlayoutparams (); 55 LP. width = (INT) (1.1f * iv. getwidth (); 56 LP. height = (INT) (1.1f * iv. getheight (); 57 58 IV. setlayoutparams (LP); 59 60 lastdistance = currentdistance; 61} else if (lastdistance-currentdistance> 5) {// 62 system. out. println (""); 63 64 framelayout. layoutparams Lp = (layoutparams) iv65. getlayoutparams (); 66 LP. width = (INT) (0.9f * iv. getwidth (); 67 LP. height = (INT) (0.9f * iv. getheight (); 68 69 IV. setlayoutparams (LP); 70 71 lastdistance = currentdistance; 72} 73} 74} 75 break; 76 case motionevent. action_up: // touch the 77 // system. out. println ("action up"); 78 break; 79} 80 81 return true; 82} 83}); 84} 85 86}
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/container" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.jikexueyuan.multouch.MainActivity" 7 tools:ignore="MergeRootFrame" > 8 9 <ImageView10 android:id="@+id/iv"11 android:layout_width="wrap_content"12 android:layout_height="wrap_content"13 android:src="@drawable/ic_launcher" />14 15 </FrameLayout>
Multi-touch operations and Image zoom-in