MultiTouch————多点触控,伸缩图片,变换图片位置

时间:2023-11-10 17:52:50

前言:当今的手机都支持多点触控功能(可以进行图片伸缩,变换位置),但是我们程序员要怎样结合硬件去实现这个功能呢?

跟随我一起,来学习这个功能

国际惯例:先上DEMO免费下载地址:http://download.csdn.net/detail/cnwutianhao/9443667

示例图片:

MultiTouch————多点触控,伸缩图片,变换图片位置

我是用Genymotion录制的,没有真机上多点触控显示的效果那么好,大家在真机上跑程序,会体会到多点触控功能。(注:Genymotion多点触控快捷键是 ctrl+鼠标指针上下拖动)

具体代码实现:


布局文件 activtiy_main.xml

     <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout 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"
         tools:context="com.cnwuth.mutiltouch.MainActivity">  

         <ImageView
             android:id="@+id/iv"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:scaleType="matrix"
             android:src="@mipmap/ic_launcher"/>  

     </RelativeLayout>  

MainActivity.java

     package com.cnwuth.mutiltouch;  

     import android.graphics.Matrix;
     import android.graphics.PointF;
     import android.support.v7.app.AppCompatActivity;
     import android.os.Bundle;
     import android.view.MotionEvent;
     import android.view.View;
     import android.widget.ImageView;  

     public class MainActivity extends AppCompatActivity implements View.OnTouchListener{  

         private ImageView mImageView;  

         //缩放控制
         private Matrix mMatrix = new Matrix();
         private Matrix savedMatrix = new Matrix();  

         //不同状态的表示
         private static final int NONE = 0;
         private static final int DRAG = 1;
         private static final int ZOOM = 2;
         private int mode = NONE;  

         //定义第一个按下的点,两只接触点的重点,以及出事的两指按下的距离:
         private PointF startPoint = new PointF();
         private PointF midPoint = new PointF();
         private float oriDis = 1f;  

         @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
             mImageView = (ImageView) findViewById(R.id.iv);
             mImageView.setOnTouchListener(this);
         }  

         @Override
         public boolean onTouch(View v, MotionEvent event) {
             ImageView view = (ImageView) v;
             switch (event.getAction() & MotionEvent.ACTION_MASK)
             {
                 //单指
                 case MotionEvent.ACTION_DOWN:
                     mMatrix.set(view.getImageMatrix());
                     savedMatrix.set(mMatrix);
                     startPoint.set(event.getX(),event.getY());
                     mode = DRAG;
                     break;
                 //双指
                 case MotionEvent.ACTION_POINTER_DOWN:
                     oriDis = distance(event);
                     if (oriDis > 10f)
                     {
                         savedMatrix.set(mMatrix);
                         midPoint = middle(event);
                         mode = ZOOM;
                     }
                     break;
                 //手指放开
                 case MotionEvent.ACTION_UP:
                 case MotionEvent.ACTION_POINTER_UP:
                     mode = NONE;
                     break;
                 //单指滑动事件
                 case MotionEvent.ACTION_MOVE:
                     if (mode == DRAG)
                     {
                         //是一个手指拖动
                         mMatrix.set(savedMatrix);
                         mMatrix.postTranslate(event.getX() - startPoint.x , event.getY() - startPoint.y);
                     }
                     else if (mode == ZOOM)
                     {
                         //两个手指滑动
                         float newDist = distance(event);
                         if (newDist > 10f)
                         {
                             mMatrix.set(savedMatrix);
                             float scale = newDist / oriDis;
                             mMatrix.postScale(scale , scale , midPoint.x , midPoint.y);
                         }
                     }
                     break;
             }
             //设置ImageView的Matrix
             view.setImageMatrix(mMatrix);
             return true;
         }  

         //计算两个触摸点之间的距离
         private float distance(MotionEvent event) {
             float x = event.getX(0) - event.getX(1);
             float y = event.getY(0) - event.getY(1);
             return (float) Math.sqrt(x * x + y * y);
         }  

         //计算两个触摸点的中点
         private PointF middle(MotionEvent event){
             float x = event.getX(0) + event.getX(1);
             float y = event.getY(0) + event.getY(1);
             return new PointF(x / 2 , y / 2);
         }
     }  

(注:环境需要在Android Studio下运行,并且SDK更新到最新,否则会出现报错)

关注我的最新动态;新浪微博 @吴天昊TnnoWu        地址:weibo.com/cnwutianhao