基于2.3
主要涉及类 import android.view.ScaleGestureDetector
接口ScaleGestureDetector.OnScaleGestureListener
ScaleGestureDetector类方法
getCurrentSpan()获取手势两点之间的距离
getScaleFactor()获取手势缩放的比例
使用:
定义ScaleGestureDetector类 在onTouch方法内调用mScaleGestureDetector.onTouchEvent(event)
实现接口OnScaleGestureListener方法
public boolean onScale(ScaleGestureDetector detector);
public boolean onScaleBegin(ScaleGestureDetector detector);
public void onScaleEnd(ScaleGestureDetector detector);
在相关方法中做相应处理
比如简单的相机预览缩放:
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
float scale = detector.getScaleFactor();
if (Float.isInfinite(scale) || Float.isNaN(scale))
return true;
zoomScale = zoomScale + (zoomScale * (scale - 1.0f) * 0.2f);
mZoomValue = (int)zoomScale - 1;
if(mZoomValue <= 0){
mZoomValue = 0;
}else if(mZoomValue >= mZoomMax){
mZoomValue = mZoomMax;
}
setCameraParametersWhenIdle(UPDATE_PARAM_ZOOM);
mHeadUpDisplay.setZoomIndex(mZoomValue);
return false;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
mFocusRectangle.setVisibility(View.GONE);
mFocusRectangle.reset();
zoomScale = mZoomValue + 1;
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
mFocusRectangle.setVisibility(View.VISIBLE);
}
需要注意onScaleBegin返回true才有后面的动作onScale