From cb16a98e5ecaa05262a02e5aa864fcf9739ac6b1 Mon Sep 17 00:00:00 2001 From: Stefan Andonian Date: Tue, 6 May 2025 11:38:25 -0700 Subject: [PATCH] Implement Jump Scroll Animation This requires a decent sized refactor. Namely, rather than relying on mCurrentPosition we need to rely more upon mLastPosition and mFinalPosition for increasing and decreasing dot size. We also need to delay the animation start if we are mega-moving to have the desired delayed dot change effect. Bug: 407711750 Test: Verified this works locally. Please see the bug for a screenrecording that shows this correct behavior working. Flag: com.android.launcher3.enable_launcher_visual_refresh Change-Id: I83ef113c17f2197e3c02e85f44cf3f05947d2adb --- src/com/android/launcher3/PagedView.java | 3 - .../pageindicators/PageIndicator.java | 7 -- .../pageindicators/PageIndicatorDots.java | 104 +++++++++++------- 3 files changed, 62 insertions(+), 52 deletions(-) diff --git a/src/com/android/launcher3/PagedView.java b/src/com/android/launcher3/PagedView.java index 92c30c0ef0..d6abb56286 100644 --- a/src/com/android/launcher3/PagedView.java +++ b/src/com/android/launcher3/PagedView.java @@ -1405,9 +1405,6 @@ public abstract class PagedView extends ViewGrou Log.d("Quickswitch", "isFling=false vel=" + velocity + " threshold=" + mEasyFlingThresholdVelocity); } - if (isFling && Flags.enableLauncherVisualRefresh() && mPageIndicator != null) { - mPageIndicator.onFling(isDeltaLeft); - } if (!mFreeScroll) { // In the case that the page is moved far to one direction and then is flung diff --git a/src/com/android/launcher3/pageindicators/PageIndicator.java b/src/com/android/launcher3/pageindicators/PageIndicator.java index 53dadec62b..0640bf3672 100644 --- a/src/com/android/launcher3/pageindicators/PageIndicator.java +++ b/src/com/android/launcher3/pageindicators/PageIndicator.java @@ -63,11 +63,4 @@ public interface PageIndicator { default void setPaintColor(int color) { // No-op by default } - - /** - * Allows the page indicator to perform actions or animations upon a fling event. - */ - default void onFling(boolean isLeftFling) { - // No-op by default - } } diff --git a/src/com/android/launcher3/pageindicators/PageIndicatorDots.java b/src/com/android/launcher3/pageindicators/PageIndicatorDots.java index 8ee6083f20..ffa8644670 100644 --- a/src/com/android/launcher3/pageindicators/PageIndicatorDots.java +++ b/src/com/android/launcher3/pageindicators/PageIndicatorDots.java @@ -46,6 +46,7 @@ import android.view.animation.OvershootInterpolator; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; +import com.android.launcher3.Flags; import com.android.launcher3.Insettable; import com.android.launcher3.R; import com.android.launcher3.Utilities; @@ -127,6 +128,7 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator private int mActivePage; private int mTotalScroll; private boolean mShouldAutoHide; + private boolean mIsMoveAnimationQueued; private int mToAlpha; /** @@ -175,23 +177,6 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator mIsRtl = Utilities.isRtl(getResources()); } - /** - * Performs a move-bounce animation upon fling. Flings happen in between 2 positions. Either we - * are coming from the right one, and going to the left, or vice versa. Ex. 3.7 current pos with - * left swipe means last=3, target=4. Slow scroll page-changes also update mLastPosition. - */ - @Override - public void onFling(boolean isLeftFling) { - mLastPosition = (int) mCurrentPosition; - float target = mLastPosition; - if (isLeftFling) { - target++; - } else { - mLastPosition++; - } - animateToPosition(target); - } - @Override public void setScroll(int currentScroll, int totalScroll) { if (currentScroll == 0 && totalScroll == 0) { @@ -222,18 +207,27 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator float scrollPerPage = (float) totalScroll / (mNumPages - 1); float position = currentScroll / scrollPerPage; - if (mAnimator == null) { + if (mIsMoveAnimationQueued) { + mCurrentPosition = position; + // For jump animations, wait until we have scrolled to within 1 page away from the + // final destination to start the animation. This has the effect of a smooth delay. + if (Math.abs(mActivePage - position) <= 1) { + animateToPosition(mActivePage); + } + } else if (mAnimator == null) { // mLastPosition is used to determine which dots should be growing / shrinking. // Update it when the scroll moves pages. Fling animations also update mLastPosition - if ((int) position != (int) mCurrentPosition) { + if (Math.abs(mLastPosition - position) > 1) { mLastPosition = Math.round(position); } + mFinalPosition = mLastPosition + (position > mLastPosition ? 1 : -1); + // Just show current position if slow scroll. Otherwise, fling animation is going CURRENT_POSITION.set(this, position); } float delta = Math.abs((int) position - position); - if (mShouldAutoHide && (delta < 0.1 || delta > 0.9)) { + if (mShouldAutoHide && !mIsMoveAnimationQueued && (delta < 0.1 || delta > 0.9)) { hideAfterDelay(); } } else { @@ -349,9 +343,11 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator if (enableLauncherVisualRefresh()) { // If user performs fling with only 11% of the distance to go, we don't want the // animation to be 150 ms long so we tether it to the scroll distance remaining. - float remainingScroll = Math.abs(position - mCurrentPosition); + // Large jump scrolls can be more than 1, so we cap the animation time. + float remainingScroll = Math.min(1, Math.abs(position - mCurrentPosition)); mAnimator.setDuration((long) (ANIMATION_DURATION * remainingScroll)); mAnimator.setInterpolator(new OvershootInterpolator()); + mIsMoveAnimationQueued = false; } mAnimator.start(); } @@ -413,6 +409,11 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator animSet.start(); } + /** + * Performs a move-bounce animation upon fling. Flings happen in between 2 positions. Either we + * are coming from the right one, and going to the left, or vice versa. Ex. 3.7 current pos with + * left swipe means last=3, target=4. Slow scroll page-changes also update mLastPosition. + */ @Override public void setActiveMarker(int activePage) { // In unfolded foldables, every page has two CellLayouts, so we need to halve the active @@ -422,7 +423,19 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator } if (mActivePage != activePage) { + mLastPosition = mActivePage; mActivePage = activePage; + + if (Flags.enableLauncherVisualRefresh()) { + // If the animation is a local snap, then immediately perform it. Otherwise, delay + // snap animation until current scroll is within 1 page of the target page. + if (Math.abs(mActivePage - mCurrentPosition) <= 1) { + animateToPosition(activePage); + } else { + mFinalPosition = activePage; + mIsMoveAnimationQueued = true; + } + } } } @@ -543,17 +556,23 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator float currentPosition = mCurrentPosition; float lastPosition = mLastPosition; + float finalPosition = mFinalPosition; if (mIsRtl) { currentPosition = mNumPages - currentPosition - 1; lastPosition = mNumPages - lastPosition - 1; + finalPosition = mNumPages - finalPosition - 1; } - float posDif = Math.abs(lastPosition - currentPosition); - float boundedPosition = (posDif > 1) - ? Math.round(currentPosition) - : currentPosition; - float bounceProgress = (posDif > 1) ? posDif - 1 : 0; - float bounceAdjustment = Math.abs(currentPosition - boundedPosition) * diameter; + + // Calculate the progress of the animation. The numerator is the distance from the + // start while the denominator is the total distance from start to end. Progress can + // be greater than 1 if performing a snap animation. In the case that the last and + // final positions are equivalent, we divide by 1 rather than 0 to avoid NaN, which + // screws up the math and has the effect of turning page indicator dots invisible. + float progress = Math.abs(currentPosition - lastPosition) + / Math.max(1, Math.abs(finalPosition - lastPosition)); + float bounceAdjustment = Math.max(progress - 1, 0) * diameter; + float alphaAdjustment = Math.max(progress - 1, 0) * nonActiveAlpha; // Here we draw the dots, one at a time from the left-most dot to the right-most dot // 1.0 => 000000 000000111111 000000 @@ -562,33 +581,35 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator // 2.0 => 000000 000000 111111000000 for (int i = 0; i < mNumPages; i++) { mPaginationPaint.setAlpha(nonActiveAlpha); - float delta = Math.abs(boundedPosition - i); - if (delta <= SHIFT_THRESHOLD) { + if (i == lastPosition && progress < SHIFT_THRESHOLD + || i == finalPosition && progress >= SHIFT_THRESHOLD) { mPaginationPaint.setAlpha(alpha); } - // If boundedPosition is 3.3, both 3 and 4 should enter this condition. - // If boundedPosition is 3, only 3 should enter this condition. - if (delta < 1) { - sTempRect.right = sTempRect.left + diameter + ((1 - delta) * diameter); + sTempRect.right = sTempRect.left + diameter + (diameter + * (i == lastPosition ? 1 - progress + : i == finalPosition ? progress : 0)); + // Save x position before adjusting right edge for bounce animation. This keeps + // the dots in the same centered locations, avoiding an accordion appearance. + x = sTempRect.right + mGapWidth; + + if (bounceAdjustment > 0) { // While the animation is shifting the active pagination dots size from // the previously active one, to the newly active dot, there is no bounce // adjustment. The bounce happens in the "Overshoot" phase of the animation. // lastPosition is used to determine when the currentPosition is just // leaving the page, or if it is in the overshoot phase. - if (boundedPosition == i && bounceProgress != 0) { - if (lastPosition < currentPosition) { + if (finalPosition == i) { + if (lastPosition < finalPosition) { sTempRect.left -= bounceAdjustment; } else { sTempRect.right += bounceAdjustment; } } - } else { - sTempRect.right = sTempRect.left + diameter; - - if (lastPosition == i && bounceProgress != 0) { - if (lastPosition > currentPosition) { + if ((lastPosition <= i && i < finalPosition) + || (finalPosition < i && i <= lastPosition)) { + if (lastPosition > finalPosition) { sTempRect.left += bounceAdjustment; } else { sTempRect.right -= bounceAdjustment; @@ -607,8 +628,7 @@ public class PageIndicatorDots extends View implements Insettable, PageIndicator } canvas.drawRoundRect(sTempRect, mDotRadius, mDotRadius, mPaginationPaint); - // TODO(b/394355070) Verify RTL experience works correctly with visual updates - sTempRect.left = sTempRect.right + mGapWidth; + sTempRect.left = x; } } else {