diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java index 38618c01..f5e99fd9 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java @@ -216,6 +216,8 @@ public class CameraViewTest extends BaseTest { cameraView.mPinchGestureLayout.setGestureType(Gesture.PINCH); } }); + // Need to set a strange value first, because we don't dispatch if oldValue = newValue. + mockController.mZoomValue = -1; mockController.mZoomChanged = false; cameraView.mapGesture(Gesture.PINCH, GestureAction.ZOOM); cameraView.dispatchTouchEvent(event); @@ -241,6 +243,8 @@ public class CameraViewTest extends BaseTest { cameraView.mScrollGestureLayout.setGestureType(Gesture.SCROLL_HORIZONTAL); } }); + // Need to set a strange value first, because we don't dispatch if oldValue = newValue. + mockController.mExposureCorrectionValue = -50f; mockController.mExposureCorrectionChanged = false; cameraView.mapGesture(Gesture.SCROLL_HORIZONTAL, GestureAction.EXPOSURE_CORRECTION); cameraView.dispatchTouchEvent(event); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index aaa5a23e..46e35d49 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -505,7 +505,9 @@ public class CameraView extends FrameLayout { case ZOOM: oldValue = mCameraController.getZoomValue(); newValue = source.scaleValue(oldValue, 0, 1); - mCameraController.setZoom(newValue, points, true); + if (newValue != oldValue) { + mCameraController.setZoom(newValue, points, true); + } break; case EXPOSURE_CORRECTION: @@ -513,8 +515,10 @@ public class CameraView extends FrameLayout { float minValue = options.getExposureCorrectionMinValue(); float maxValue = options.getExposureCorrectionMaxValue(); newValue = source.scaleValue(oldValue, minValue, maxValue); - float[] bounds = new float[]{minValue, maxValue}; - mCameraController.setExposureCorrection(newValue, bounds, points, true); + if (newValue != oldValue) { + float[] bounds = new float[]{minValue, maxValue}; + mCameraController.setExposureCorrection(newValue, bounds, points, true); + } break; } } diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/GestureLayout.java b/cameraview/src/main/views/com/otaliastudios/cameraview/GestureLayout.java index 6f1fcc36..5932198b 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/GestureLayout.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/GestureLayout.java @@ -7,12 +7,12 @@ import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; -/** - * Using Views to address the need to draw stuff, but think about it. - * Simple classes could be enough. - */ abstract class GestureLayout extends FrameLayout { + // The number of possible values between minValue and maxValue, for the scaleValue method. + // We could make this non-static (e.g. larger granularity for exposure correction). + private final static int GRANULARITY = 50; + protected boolean mEnabled; protected Gesture mType; protected PointF[] mPoints; @@ -48,5 +48,22 @@ abstract class GestureLayout extends FrameLayout { return mPoints; } + // Implementors should call capValue at the end. public abstract float scaleValue(float currValue, float minValue, float maxValue); + + // Checks for newValue to be between minValue and maxValue, + // and checks that it is 'far enough' from the oldValue, in order + // to reduce useless updates. + protected static float capValue(float oldValue, float newValue, float minValue, float maxValue) { + if (newValue < minValue) newValue = minValue; + if (newValue > maxValue) newValue = maxValue; + + float distance = (maxValue - minValue) / (float) GRANULARITY; + float half = distance / 2; + if (newValue >= oldValue - half && newValue <= oldValue + half) { + // Too close! Return the oldValue. + return oldValue; + } + return newValue; + } } diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java b/cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java index 08c0cd62..8ba8ddec 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java @@ -87,10 +87,6 @@ class PinchGestureLayout extends GestureLayout { } else if (add < 0) { add *= (currValue - minValue); } Nope, I don't like this, it slows everything down. */ - - float newValue = currValue + add; - if (newValue < minValue) newValue = minValue; - if (newValue > maxValue) newValue = maxValue; - return newValue; + return capValue(currValue, currValue + add, minValue, maxValue); } } diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java b/cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java index 09cb84f9..d0c8a25e 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java @@ -85,16 +85,9 @@ class ScrollGestureLayout extends GestureLayout { // ^ This works well if minValue = 0, maxValue = 1. // Account for the different range: delta *= (maxValue - minValue); // -(max-min) ... (max-min) + delta *= 2; // Add some sensitivity. - // Add some sensitivity. - delta *= 2; - - // Cap - float newValue = currValue + delta; - if (newValue < minValue) newValue = minValue; - if (newValue > maxValue) newValue = maxValue; - LOG.i("curr="+currValue, "min="+minValue, "max="+maxValue, "out="+newValue); - return newValue; + return capValue(currValue, currValue + delta, minValue, maxValue); } }