I have a custom surfaceview for my CameraPreview in my app, and I am trying to implement Pinch zoom, by implementing these two methods:
@Override
public boolean onTouchEvent(MotionEvent event) {
Camera camera = getCamera();
if (camera == null) {
return true;
}
Camera.Parameters params = camera.getParameters();
int action = event.getAction();
if (event.getPointerCount() > 1) {
if (action == MotionEvent.ACTION_POINTER_DOWN) {
MCLog.v(TAG, "Single ");
mDist = getFingerSpacing(event);
MCLog.w(TAG, "Original distance " + mDist);
} else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
camera.cancelAutoFocus();
handleZoom(event, params);
}
} else {
if (action == MotionEvent.ACTION_UP) {
mFirstTime = false;
handleFocus(event, params);
}
}
return true;
}
private void handleZoom(MotionEvent event, Camera.Parameters params) {
if(mFirstTime) {
mDist = getFingerSpacing(event);
mFirstTime = false;
return;
}
List<Integer> zoomRatios = params.getZoomRatios();
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
double spacing = getFingerSpacing(event);
MCLog.w(TAG, String.format("Old zoom is: %s", zoom));
//Percentage of displacement
MCLog.w(TAG, String.format("Original distance is: %s, new displacement is %s", mDist, spacing));
double percentage = (mDist + spacing)/mDist;
if(mDist > spacing)
{
percentage *= -1;
}
MCLog.w(TAG, String.format("Percentage is: %s", percentage));
zoom = new Double(zoom + percentage).intValue();
MCLog.w(TAG, String.format("New zoom is: %s", zoom));
if (zoom > maxZoom) {
zoom = maxZoom;
}
if (zoom < 0) {
zoom = 0;
}
mDist = spacing;
params.setZoom(zoom);
if (mZoomListener != null) {
mZoomListener.onZoomChanged(zoomRatios.get(zoom));
}
getCamera().setParameters(params);
}
This seems to be working, however the zoom has some slight delay that gets longer the more I zoom into the image. Like I would stop pinching and the image would still keep zooming in.
I couldnt find any implementation for pinch zoom in the camera besides this one, so maybe this is doing something wrong.