Tap to stop the carousel
When the carousel is moving faster than 15 degrees per
second, a tap will stop the motion but not be interpreted
as a selection. Slower than that, it will be considered a
selection.
This is an improvement on an earlier implementation because
it lets through selections when the carousel is still
moving slowly.
Bug: 3179984
Change-Id: I27b55cf88e20d4ef1383ec0e4144f65a101f37fc
diff --git a/carousel/java/com/android/ex/carousel/carousel.rs b/carousel/java/com/android/ex/carousel/carousel.rs
index 939cb85..1de38de 100644
--- a/carousel/java/com/android/ex/carousel/carousel.rs
+++ b/carousel/java/com/android/ex/carousel/carousel.rs
@@ -896,7 +896,8 @@
static float lastAngle = 0.0f;
static float2 lastPosition;
static bool animating = false;
-static float velocityThreshold = 0.1f * M_PI / 180.0f;
+static float stopVelocity = 0.1f * M_PI / 180.0f; // slower than this: carousel stops
+static float selectionVelocity = 15.0f * M_PI / 180.0f; // faster than this: tap won't select
static float velocityTracker;
static int velocityTrackerCount;
static float mass = 5.0f; // kg
@@ -977,13 +978,13 @@
{
touchPosition = lastPosition = (float2) { x, y };
lastAngle = hitAngle(x,y, &lastAngle) ? lastAngle : 0.0f;
+ enableSelection = fabs(velocity) < selectionVelocity;
velocity = 0.0f;
velocityTracker = 0.0f;
velocityTrackerCount = 0;
touchTime = lastTime = eventTime;
touchBias = bias;
isDragging = true;
- enableSelection = true;
animatedSelection = doSelection(x, y); // used to provide visual feedback on touch
}
@@ -1013,7 +1014,7 @@
// TODO: move velocity tracking to Java
velocity = velocityTrackerCount > 0 ?
(velocityTracker / velocityTrackerCount) : 0.0f; // avg velocity
- if (fabs(velocity) > velocityThreshold) {
+ if (fabs(velocity) > stopVelocity) {
animating = true;
}
}
@@ -1305,7 +1306,7 @@
velocity = momentum / mass;
bias += velocity * dt;
}
- return fabs(velocity) > velocityThreshold;
+ return fabs(velocity) > stopVelocity;
}
static float easeOut(float x)