From 5b139c32464b56c30cd8d7c2304acf044a65282a Mon Sep 17 00:00:00 2001 From: Joshua Tsuji Date: Thu, 30 Jan 2020 18:03:09 -0500 Subject: [PATCH] Adds estimateFlingEndValue to PhysicsAnimator. Flings do not have a set end value, and decelerate via friction until coming to a halt. However, it's sometimes useful to have an estimate of where that might be - similar logic is used by flingMustReachMinOrMax. Test: atest SystemUITests Change-Id: I271a1587a321b97fbbdad1c00605a17342b6e694 --- .../systemui/util/animation/PhysicsAnimator.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/SystemUI/src/com/android/systemui/util/animation/PhysicsAnimator.kt b/packages/SystemUI/src/com/android/systemui/util/animation/PhysicsAnimator.kt index cfd77be9303dd..f4157f21e1583 100644 --- a/packages/SystemUI/src/com/android/systemui/util/animation/PhysicsAnimator.kt +++ b/packages/SystemUI/src/com/android/systemui/util/animation/PhysicsAnimator.kt @@ -901,6 +901,23 @@ class PhysicsAnimator private constructor (val target: T) { verboseLogging = debug } + /** + * Estimates the end value of a fling that starts at the given value using the provided + * start velocity and fling configuration. + * + * This is only an estimate. Fling animations use a timing-based physics simulation that is + * non-deterministic, so this exact value may not be reached. + */ + @JvmStatic + fun estimateFlingEndValue( + startValue: Float, + startVelocity: Float, + flingConfig: FlingConfig + ): Float { + val distance = startVelocity / (flingConfig.friction * FLING_FRICTION_SCALAR_MULTIPLIER) + return Math.min(flingConfig.max, Math.max(flingConfig.min, startValue + distance)) + } + @JvmStatic fun getReadablePropertyName(property: FloatPropertyCompat<*>): String { return when (property) {