View main = (View) findViewById(R.id.linearLayout);
main.setOnTouchListener(new OnTouchListener() {
int mode = -1;
int DRAG = 1;
int NONE = 0;
int ZOOM = 2;
float oldDist;
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
textView.setText("ACTION_POINTER_DOWN:"+oldDist+"");
if (oldDist > 10f) {
mode = ZOOM;
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM) {
// 正在移动的点距初始点的距离
float newDist = spacing(event);
textView.setText("ACTION_MOVE:"+newDist+"");
}
break;
}
return true;
}
});
----------------------------
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
------------------------------