Add discrete granularity to pinch and scroll events

pull/170/head
Mattia Iavarone 8 years ago
parent ef1a4ecc65
commit 2d51c7f58f
  1. 4
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java
  2. 4
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  3. 25
      cameraview/src/main/views/com/otaliastudios/cameraview/GestureLayout.java
  4. 6
      cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java
  5. 11
      cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.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);

@ -505,7 +505,9 @@ public class CameraView extends FrameLayout {
case ZOOM:
oldValue = mCameraController.getZoomValue();
newValue = source.scaleValue(oldValue, 0, 1);
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);
if (newValue != oldValue) {
float[] bounds = new float[]{minValue, maxValue};
mCameraController.setExposureCorrection(newValue, bounds, points, true);
}
break;
}
}

@ -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;
}
}

@ -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);
}
}

@ -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);
}
}

Loading…
Cancel
Save