标签:
前言:当今的手机都支持多点触控功能(可以进行图片伸缩,变换位置),但是我们程序员要怎样结合硬件去实现这个功能呢?
跟随我一起,来学习这个功能
国际惯例:先上DEMO免费下载地址:http://download.csdn.net/detail/cnwutianhao/9443667
示例图片:
我是用Genymotion录制的,没有真机上多点触控显示的效果那么好,大家在真机上跑程序,会体会到多点触控功能。(注:Genymotion多点触控快捷键是 ctrl+鼠标指针上下拖动)
具体代码实现:
布局文件 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 //缩放控制 16 private Matrix mMatrix = new Matrix(); 17 private Matrix savedMatrix = new Matrix(); 18 19 //不同状态的表示 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 //定义第一个按下的点,两只接触点的重点,以及出事的两指按下的距离: 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 //双指 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 //手指放开 61 case MotionEvent.ACTION_UP: 62 case MotionEvent.ACTION_POINTER_UP: 63 mode = NONE; 64 break; 65 //单指滑动事件 66 case MotionEvent.ACTION_MOVE: 67 if (mode == DRAG) 68 { 69 //是一个手指拖动 70 mMatrix.set(savedMatrix); 71 mMatrix.postTranslate(event.getX() - startPoint.x , event.getY() - startPoint.y); 72 } 73 else if (mode == ZOOM) 74 { 75 //两个手指滑动 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 //设置ImageView的Matrix 87 view.setImageMatrix(mMatrix); 88 return true; 89 } 90 91 //计算两个触摸点之间的距离 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 //计算两个触摸点的中点 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 }
(注:环境需要在Android Studio下运行,并且SDK更新到最新,否则会出现报错)
关注我的最新动态;新浪微博 @吴天昊TnnoWu 地址:weibo.com/cnwutianhao
MultiTouch————多点触控,伸缩图片,变换图片位置
标签:
原文地址:http://www.cnblogs.com/cnwutianhao/p/5219188.html