1 package com.itheima.touch; 2 3 import android.app.Activity; 4 import android.graphics.Matrix; 5 import android.graphics.PointF; 6 import android.os.Bundle; 7 import android.view.MotionEvent; 8 import android.view.View; 9 import android.view.View.OnTouchListener; 10 import android.widget.ImageView; 11 12 public class MainActivity extends Activity { 13 14 private ImageView imageView; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 imageView = (ImageView) findViewById(R.id.imageView); 22 imageView.setOnTouchListener(new MyOnTouchListener()); // 添加触摸监听器 23 } 24 25 private class MyOnTouchListener implements OnTouchListener { 26 private float x; // 图片移动前的x轴坐标 27 private float y; // 图片移动前的y轴坐标 28 private Matrix newMatrix = new Matrix(); // 用来移动图片的矩阵 29 private Matrix oldMatrix = new Matrix(); // 图片移动前的矩阵 30 private int type; // 操作类型, 一根手指触摸还是两根手指触摸 31 private float start; // 第二根手指按下时的距离 32 private float end; // 两根手指移动后的距离 33 private PointF point; // 放大时的中心点 34 35 public boolean onTouch(View v, MotionEvent event) { 36 switch (event.getAction() & MotionEvent.ACTION_MASK) { 37 case MotionEvent.ACTION_DOWN: 38 type = 1; 39 x = event.getX(); 40 y = event.getY(); 41 oldMatrix.set(imageView.getImageMatrix()); 42 break; 43 case MotionEvent.ACTION_POINTER_DOWN: 44 type = 2; 45 start = countDistance(event); // 计算第二根手指按下时两点之间的距离 46 point = countPoint(event); // 计算中心点 47 oldMatrix.set(imageView.getImageMatrix()); 48 break; 49 case MotionEvent.ACTION_MOVE: 50 newMatrix.set(oldMatrix); 51 if (type == 1) { // 1根手指触摸 52 newMatrix.postTranslate(event.getX() - x, event.getY() - y); 53 } else { // 2跟手指触摸 54 end = countDistance(event); // 计算结束时距离 55 float scale = end / start; // 计算缩放比例 56 newMatrix.postScale(scale, scale, point.x, point.y); // 对模型进行缩放 57 } 58 break; 59 } 60 imageView.setImageMatrix(newMatrix); // 改变图片 61 return true; 62 } 63 } 64 65 public float countDistance(MotionEvent event) { 66 float a = event.getX(1) - event.getX(0); // x轴距离 67 float b = event.getY(1) - event.getY(0); // y轴距离 68 return (float) Math.sqrt(a * a + b * b); // 勾股定理 69 } 70 71 public PointF countPoint(MotionEvent event) { 72 float x = (event.getX(0) + event.getX(1)) / 2; // x轴中间点 73 float y = (event.getY(0) + event.getY(1)) / 2; // y轴中间点 74 return new PointF(x, y); 75 } 76 77 78 /* 79 private class MyOnTouchListener implements OnTouchListener { 80 private float x; 81 private float y; 82 private Matrix oldMatrix = new Matrix(); 83 private Matrix newMatrix = new Matrix(); // 用来操作图片的模型 84 85 public boolean onTouch(View v, MotionEvent event) { 86 switch (event.getAction()) { // 判断触摸动作的类型 87 case MotionEvent.ACTION_DOWN: // 按下时 88 x = event.getX(); // 获取x轴坐标 89 y = event.getY(); // 获取y轴坐标 90 oldMatrix.set(imageView.getImageMatrix()); // 用模型记住图片所在位置 91 break; 92 case MotionEvent.ACTION_MOVE: // 移动时 93 newMatrix.set(oldMatrix); // 用另一个模型记住按下时的位置 94 newMatrix.postTranslate(event.getX() - x, event.getY() - y); // 移动模型 95 break; 96 } 97 imageView.setImageMatrix(newMatrix); // 把图片放到了移动后的模型中 98 return true; 99 } 100 } 101 */ 102 103 }
时间: 2024-11-09 10:49:40