MultiTouch ---- multi-touch, scaling images, changing image positions,

Source: Internet
Author: User
Tags gety

MultiTouch ---- multi-touch, scaling images, changing image positions,

Today's mobile phones support multi-touch functions (images can be scaled and positions can be changed). But how can we use hardware to implement this function?

Follow me to learn this function

International practice: first DEMO free: http://download.csdn.net/detail/cnwutianhao/9443667

Example image:

 

I recorded it with Genymotion, but it didn't work as well as the multi-touch display on the real machine. You can run the program on the real machine and enjoy the multi-touch function. (Note: The Genymotion multi-touch shortcut is ctrl + move the cursor up and down)


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"   3         xmlns:tools="http://schemas.android.com/tools"   4         android:layout_width="match_parent"   5         android:layout_height="match_parent"   6         tools:context="com.cnwuth.mutiltouch.MainActivity">   7        8         <ImageView   9             android:id="@+id/iv"  10             android:layout_width="match_parent"  11             android:layout_height="match_parent"  12             android:scaleType="matrix"  13             android:src="@mipmap/ic_launcher"/>  14       15     </RelativeLayout>  

 

MainActivity. java

 

1 package com. cnwuth. mutiltouch; 2 3 import android. graphics. matrix; 4 import android. graphics. pointF; 5 import android. support. v7.app. appCompatActivity; 6 import android. OS. bundle; 7 import android. view. motionEvent; 8 import android. view. view; 9 import android. widget. imageView; 10 11 public class MainActivity extends AppCompatActivity implements View. onTouchListener {12 13 private ImageView mImageView; 14 15 // scaling Control 16 private Matrix mMatrix = new Matrix (); 17 private Matrix savedMatrix = new Matrix (); 18 19 // indicate 20 private static final int NONE = 0; 21 private static final int DRAG = 1; 22 private static final int ZOOM = 2; 23 private int mode = NONE; 24 25 // defines the point of the first press, the focus of the two touchpoints, and the distance between the two fingers that have an accident: 26 private PointF startPoint = new PointF (); 27 private PointF midPoint = new PointF (); 28 private float oriDis = 1f; 29 30 @ Override 31 protected void onCreate (Bundle savedInstanceState) {32 super. onCreate (savedInstanceState); 33 setContentView (R. layout. activity_main); 34 mImageView = (ImageView) findViewById (R. id. iv); 35 mImageView. setOnTouchListener (this); 36} 37 38 @ Override 39 public boolean onTouch (View v, MotionEvent event) {40 ImageView view = (ImageView) v; 41 switch (event. getAction () & MotionEvent. ACTION_MASK) 42 {43 // 44 case MotionEvent. ACTION_DOWN: 45 mMatrix. set (view. getImageMatrix (); 46 savedMatrix. set (mMatrix); 47 startPoint. set (event. getX (), event. getY (); 48 mode = DRAG; 49 break; 50 // double-finger 51 case MotionEvent. ACTION_POINTER_DOWN: 52 oriDis = distance (event); 53 if (oriDis> 10f) 54 {55 savedMatrix. set (mMatrix); 56 midPoint = middle (event); 57 mode = ZOOM; 58} 59 break; 60 // open 61 case MotionEvent by finger. ACTION_UP: 62 case MotionEvent. ACTION_POINTER_UP: 63 mode = NONE; 64 break; 65 // single-finger sliding event 66 case MotionEvent. ACTION_MOVE: 67 if (mode = DRAG) 68 {69 // is a finger dragging 70 mMatrix. set (savedMatrix); 71 mMatrix. postTranslate (event. getX ()-startPoint. x, event. getY ()-startPoint. y); 72} 73 else if (mode = ZOOM) 74 {75 // two fingers slide 76 float newDist = distance (event); 77 if (newDist> 10f) 78 {79 mMatrix. set (savedMatrix); 80 float scale = newDist/oriDis; 81 mMatrix. postScale (scale, scale, midPoint. x, midPoint. y); 82} 83} 84 break; 85} 86 // set the Matrix 87 view of ImageView. setImageMatrix (mMatrix); 88 return true; 89} 90 91 // calculate the distance between two touch points 92 private float distance (MotionEvent event) {93 float x = event. getX (0)-event. getX (1); 94 float y = event. getY (0)-event. getY (1); 95 return (float) Math. sqrt (x * x + y * y); 96} 97 98 // calculate the midpoint 99 private PointF middle (MotionEvent event) {100 float x = event. getX (0) + event. getX (1); 101 float y = event. getY (0) + event. getY (1); 102 return new PointF (x/2, y/2); 103} 104}

 

(Note: The environment must run in Android Studio and the SDK is updated to the latest version. Otherwise, an error will occur)


Follow my latest news; Sina Weibo @ Wu tianhao TnnoWu address: weibo.com/cnwutianhao

 

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.