多点触控的TouchListener

时间:2022-07-20 16:37:39

记录一个用于多点触控,移动放大缩小的TouchListener

  1 import android.graphics.Matrix; 
  2 import android.graphics.PointF; 
  3 import android.util.FloatMath; 
  4 import android.view.MotionEvent; 
  5 import android.view.View; 
  6 import android.view.View.OnTouchListener; 
  7 import android.widget.ImageView; 
  8 
  9 public class MulitPointTouchListener implements OnTouchListener { 
 10 
 11     private static final String TAG = "Touch"; 
 12     // These matrices will be used to move and zoom image 
 13     Matrix matrix = new Matrix(); 
 14     Matrix savedMatrix = new Matrix(); 
 15 
 16     // We can be in one of these 3 states 
 17     static final int NONE = 0; 
 18     static final int DRAG = 1; 
 19     static final int ZOOM = 2; 
 20     int mode = NONE; 
 21 
 22     // Remember some things for zooming 
 23     PointF start = new PointF(); 
 24     PointF mid = new PointF(); 
 25     float oldDist = 1f; 
 26 
 27     @Override 
 28     public boolean onTouch(View v, MotionEvent event) { 
 29 
 30             ImageView view = (ImageView) v; 
 31             // Log.e("view_width", 
 32             // view.getImageMatrix()..toString()+"*"+v.getWidth()); 
 33             // Dump touch event to log 
 34             dumpEvent(event); 
 35 
 36             // Handle touch events here... 
 37             switch (event.getAction() & MotionEvent.ACTION_MASK) { 
 38             case MotionEvent.ACTION_DOWN: 
 39 
 40                     matrix.set(view.getImageMatrix()); 
 41                     savedMatrix.set(matrix); 
 42                     start.set(event.getX(), event.getY()); 
 43                     //Log.d(TAG, "mode=DRAG"); 
 44                     mode = DRAG; 
 45 
 46                    
 47                     //Log.d(TAG, "mode=NONE"); 
 48                     break; 
 49             case MotionEvent.ACTION_POINTER_DOWN: 
 50                     oldDist = spacing(event); 
 51                     //Log.d(TAG, "oldDist=" + oldDist); 
 52                     if (oldDist > 10f) { 
 53                             savedMatrix.set(matrix); 
 54                             midPoint(mid, event); 
 55                             mode = ZOOM; 
 56                             //Log.d(TAG, "mode=ZOOM"); 
 57                     } 
 58                     break; 
 59             case MotionEvent.ACTION_UP: 
 60             case MotionEvent.ACTION_POINTER_UP: 
 61                     mode = NONE; 
 62                     //Log.e("view.getWidth", view.getWidth() + ""); 
 63                     //Log.e("view.getHeight", view.getHeight() + ""); 
 64 
 65                     break; 
 66             case MotionEvent.ACTION_MOVE: 
 67                     if (mode == DRAG) { 
 68                             // ... 
 69                             matrix.set(savedMatrix); 
 70                             matrix.postTranslate(event.getX() - start.x, event.getY() 
 71                                             - start.y); 
 72                     } else if (mode == ZOOM) { 
 73                             float newDist = spacing(event); 
 74                             //Log.d(TAG, "newDist=" + newDist); 
 75                             if (newDist > 10f) { 
 76                                     matrix.set(savedMatrix); 
 77                                     float scale = newDist / oldDist; 
 78                                     matrix.postScale(scale, scale, mid.x, mid.y); 
 79                             } 
 80                     } 
 81                     break; 
 82             } 
 83 
 84             view.setImageMatrix(matrix); 
 85             return true; // indicate event was handled 
 86     } 
 87 
 88     private void dumpEvent(MotionEvent event) { 
 89             String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", 
 90                             "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" }; 
 91             StringBuilder sb = new StringBuilder(); 
 92             int action = event.getAction(); 
 93             int actionCode = action & MotionEvent.ACTION_MASK; 
 94             sb.append("event ACTION_").append(names[actionCode]); 
 95             if (actionCode == MotionEvent.ACTION_POINTER_DOWN 
 96                             || actionCode == MotionEvent.ACTION_POINTER_UP) { 
 97                     sb.append("(pid ").append( 
 98                                     action >> MotionEvent.ACTION_POINTER_ID_SHIFT); 
 99                     sb.append(")"); 
100             } 
101             sb.append("["); 
102             for (int i = 0; i < event.getPointerCount(); i++) { 
103                     sb.append("#").append(i); 
104                     sb.append("(pid ").append(event.getPointerId(i)); 
105                     sb.append(")=").append((int) event.getX(i)); 
106                     sb.append(",").append((int) event.getY(i)); 
107                     if (i + 1 < event.getPointerCount()) 
108                             sb.append(";"); 
109             } 
110             sb.append("]"); 
111             //Log.d(TAG, sb.toString()); 
112     } 
113 
114    
115     private float spacing(MotionEvent event) { 
116             float x = event.getX(0) - event.getX(1); 
117             float y = event.getY(0) - event.getY(1); 
118             return FloatMath.sqrt(x * x + y * y); 
119     } 
120 
121    
122     private void midPoint(PointF point, MotionEvent event) { 
123             float x = event.getX(0) + event.getX(1); 
124             float y = event.getY(0) + event.getY(1); 
125             point.set(x / 2, y / 2); 
126     } 
127 
128 
129 }