class MyTouchListener implements OnTouchListener {
private Context ctx;
private int mode;
private Float oldDist;
public MyTouchListener(Context ctx) {
this.ctx = ctx;
}
//用于获得二指间的距离
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);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = 1;//当屏幕上唯一的点被放开时触发
break;
case MotionEvent.ACTION_UP:
mode = 0;//当屏幕上唯一的点被放开时触发
break;
case MotionEvent.ACTION_POINTER_UP:
mode -= 1;//:当屏幕上有多个点被按住,松开其中一个点时触发(即非最后一个点被放开时)。
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);// 两点按下时的距离
mode += 1;//当屏幕上已经有一个点被按住,此时再按下其他点时触发。
break;
case MotionEvent.ACTION_MOVE:
if (mode >= 2) {
System.out.println(oldDist);
float newDist = spacing(event);
System.out.println(newDist);
if (newDist > oldDist) {
Camera.Parameters p=m_cCamera.getParameters();
int maxZoom=p.getMaxZoom();
if(initZoom+1<maxZoom){
initZoom+=1;
}
p.setZoom(initZoom);
m_cCamera.setParameters(p);
m_cCamera.startPreview();
}
if (newDist < oldDist) {
Camera.Parameters p=m_cCamera.getParameters();
if(initZoom-1>0){
initZoom-=1;
}
p.setZoom(initZoom);
m_cCamera.setParameters(p);
m_cCamera.startPreview();
}
}
break;
}
return true;
}
}