1 /** 2 *Button的onTouch,onClick,onLongClick事件发生先后顺序和关联: 3 *一,onTouch返回false 4 *首先是onTouch事件的down事件发生,此时,如果长按,触发onLongClick事件; 5 *然后是onTouch事件的up事件发生,up完毕,最后触发onClick事件。 6 * 7 *二,onTouch返回true 8 *首先是onTouch事件的down事件发生,然后是onTouch事件的up事件发生; 9 *期间不触发onClick和onLongClick事件 10 * 11 *三,onTouch:down返回true,up返回false:结果同二。 12 *机制分析: 13 * onTouch事件中:down事件返回值标记此次事件是否为点击事件(返回false,是点击事件;返回true,不记为点击事件), 14 *而up事件标记此次事件结束时间,也就是判断是否为长按。 15 *只要当down返回true时候,系统将不把本次事件记录为点击事件,也就不会触发onClick或者onLongClick事件了。 16 *因此尽管当up的时候返回false,系统也不会继续触发onClick事件了。 17 * 18 *四,onTouch:down返回false,up返回true: 19 *首先是onTouch事件的down事件发生,此时: 20 *长按,触发onLongClick事件,然后是onTouch事件的up事件发生,完毕。 21 *短按,先触发onTouch的up事件, 到一定时间后,自动触发onLongClick事件。 22 *机制分析: 23 * onTouch事件中:down事件返回值标记此次事件是否为点击事件(返回false,是点击事件;返回true,不记为点击事件),而up事件标记此次事件结束时间,也就是判断是否为长按。 24 * 当down返回false,标记此次事件为点击事件,而up返回了true,则表示此次事件一直没有结束,也就是一直长按下去了,达到长按临界时间后,自然触发长按事件,而onClick事件没有触发到 25 */ 26 public class ImageZoomActivity extends Activity implements OnTouchListener { 27 private static final String TAG = "Touch"; 28 // These matrices will be used to move and zoom image 29 Matrix matrix = new Matrix(); 30 Matrix savedMatrix = new Matrix(); 31 32 // We can be in one of these 3 states 33 static final int NONE = 0; 34 static final int DRAG = 1; 35 static final int ZOOM = 2; 36 int mode = NONE; 37 38 // Remember some things for zooming 39 PointF start = new PointF(); 40 PointF mid = new PointF(); 41 float oldDist = 1f; 42 private ImageView view; 43 private boolean keyUpDown=false; 44 private int timer=0; 45 46 /* (non-Javadoc) 47 * @see android.app.Activity#onCreate(android.os.Bundle) 48 */ 49 @Override 50 public void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 requestWindowFeature(Window.FEATURE_NO_TITLE); 53 setContentView(R.layout.touch); 54 view = (ImageView) findViewById(R.id.imageView); 55 Intent intent=getIntent(); 56 // Drawable drawable=loadImgByFilePath(intent.getStringExtra("toZoom")); 57 view.setImageURI(Uri.parse(intent.getStringExtra("toZoom"))); 58 view.setOnTouchListener(this); 59 } 60 61 private Handler clickHandler = new Handler(){ 62 @Override 63 public void handleMessage(Message msg) { 64 if(msg.what==0){ 65 keyUpDown=true; 66 keyUpDownListener(); 67 }else if(msg.what==1){ 68 keyUpDown=false; 69 if(timer<=1) 70 ImageZoomActivity.this.finish();//此处即为OnClick处理对应部分,只需在此处添中处理代码即可. 71 else timer=0; 72 } 73 } 74 }; 75 private int keyUpDownListener() { 76 new Thread(){ 77 public void run(){ 78 while(keyUpDown){ 79 try { 80 sleep(200); 81 timer++; 82 Log.d("info","timing:timer="+timer); 83 } catch (InterruptedException e) { 84 e.printStackTrace(); 85 } 86 } 87 } 88 }.start(); 89 return timer; 90 } 91 92 @Override 93 public boolean onTouch(View v, MotionEvent event) { 94 ImageView view = (ImageView) v; 95 96 // Dump touch event to log 97 // dumpEvent(event); 98 99 // Handle touch events here... 100 switch (event.getAction() & MotionEvent.ACTION_MASK) 101 { 102 case MotionEvent.ACTION_DOWN: 103 savedMatrix.set(matrix); 104 //設置初始點位置 105 start.set(event.getX(), event.getY()); 106 Log.d(TAG, "mode=DRAG"); 107 clickHandler.sendEmptyMessage(0); 108 mode = DRAG; 109 break; 110 case MotionEvent.ACTION_POINTER_DOWN: 111 oldDist = spacing(event); 112 Log.d(TAG, "oldDist=" + oldDist); 113 if (oldDist > 10f) { 114 savedMatrix.set(matrix); 115 midPoint(mid, event); 116 mode = ZOOM; 117 Log.d(TAG, "mode=ZOOM"); 118 } 119 break; 120 case MotionEvent.ACTION_UP:clickHandler.sendEmptyMessage(1); 121 case MotionEvent.ACTION_POINTER_UP: 122 mode = NONE; 123 Log.d(TAG, "mode=NONE"); 124 break; 125 case MotionEvent.ACTION_MOVE: 126 if (mode == DRAG) { 127 // ... 128 matrix.set(savedMatrix); 129 matrix.postTranslate(event.getX() - start.x, 130 event.getY() - start.y); 131 } 132 else if (mode == ZOOM) { 133 float newDist = spacing(event); 134 Log.d(TAG, "newDist=" + newDist); 135 if (newDist > 10f) { 136 matrix.set(savedMatrix); 137 float scale = newDist / oldDist; 138 matrix.postScale(scale, scale, mid.x, mid.y); 139 } 140 } 141 break; 142 } 143 144 view.setImageMatrix(matrix); 145 return true; // indicate event was handled 146 } 147 148 /** Determine the space between the first two fingers */ 149 private float spacing(MotionEvent event) { 150 float x = event.getX(0) - event.getX(1); 151 float y = event.getY(0) - event.getY(1); 152 return FloatMath.sqrt(x * x + y * y); 153 } 154 155 /** Calculate the mid point of the first two fingers */ 156 private void midPoint(PointF point, MotionEvent event) { 157 float x = event.getX(0) + event.getX(1); 158 float y = event.getY(0) + event.getY(1); 159 point.set(x / 2, y / 2); 160 } 161 }