Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1de09eea authored by Kateryna Ivanova's avatar Kateryna Ivanova Committed by Automerger Merge Worker
Browse files

Migrate Interpolators from Launcher3 to the public animation library am: d3a267de

parents 863fa55d d3a267de
Loading
Loading
Loading
Loading
+176 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.PathInterpolator;

/**
@@ -59,6 +60,26 @@ public class Interpolators {
            0.05f, 0.7f, 0.1f, 1f);


    public static final Interpolator EXAGGERATED_EASE;
    static {
        Path exaggeratedEase = new Path();
        exaggeratedEase.moveTo(0, 0);
        exaggeratedEase.cubicTo(0.05f, 0f, 0.133333f, 0.08f, 0.166666f, 0.4f);
        exaggeratedEase.cubicTo(0.225f, 0.94f, 0.5f, 1f, 1f, 1f);
        EXAGGERATED_EASE = new PathInterpolator(exaggeratedEase);
    }

    public static final Interpolator INSTANT = t -> 1;
    /**
     * All values of t map to 0 until t == 1. This is primarily useful for setting view visibility,
     * which should only happen at the very end of the animation (when it's already hidden).
     */
    public static final Interpolator FINAL_FRAME = t -> t < 1 ? 0 : 1;

    public static final Interpolator OVERSHOOT_0_75 = new OvershootInterpolator(0.75f);
    public static final Interpolator OVERSHOOT_1_2 = new OvershootInterpolator(1.2f);
    public static final Interpolator OVERSHOOT_1_7 = new OvershootInterpolator(1.7f);

    /*
     * ============================================================================================
     * Standard interpolators.
@@ -131,11 +152,27 @@ public class Interpolators {
    public static final Interpolator FAST_OUT_SLOW_IN_REVERSE =
            new PathInterpolator(0.8f, 0f, 0.6f, 1f);
    public static final Interpolator SLOW_OUT_LINEAR_IN = new PathInterpolator(0.8f, 0f, 1f, 1f);
    public static final Interpolator AGGRESSIVE_EASE = new PathInterpolator(0.2f, 0f, 0f, 1f);
    public static final Interpolator AGGRESSIVE_EASE_IN_OUT = new PathInterpolator(0.6f,0, 0.4f, 1);

    public static final Interpolator DECELERATED_EASE = new PathInterpolator(0, 0, .2f, 1f);
    public static final Interpolator ACCELERATED_EASE = new PathInterpolator(0.4f, 0, 1f, 1f);
    public static final Interpolator PREDICTIVE_BACK_DECELERATED_EASE =
            new PathInterpolator(0, 0, 0, 1f);
    public static final Interpolator ALPHA_IN = new PathInterpolator(0.4f, 0f, 1f, 1f);
    public static final Interpolator ALPHA_OUT = new PathInterpolator(0f, 0f, 0.8f, 1f);
    public static final Interpolator ACCELERATE = new AccelerateInterpolator();
    public static final Interpolator ACCELERATE_0_5 = new AccelerateInterpolator(0.5f);
    public static final Interpolator ACCELERATE_0_75 = new AccelerateInterpolator(0.75f);
    public static final Interpolator ACCELERATE_1_5 = new AccelerateInterpolator(1.5f);
    public static final Interpolator ACCELERATE_2 = new AccelerateInterpolator(2);
    public static final Interpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
    public static final Interpolator DECELERATE = new DecelerateInterpolator();
    public static final Interpolator DECELERATE_1_5 = new DecelerateInterpolator(1.5f);
    public static final Interpolator DECELERATE_1_7 = new DecelerateInterpolator(1.7f);
    public static final Interpolator DECELERATE_2 = new DecelerateInterpolator(2);
    public static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f);
    public static final Interpolator DECELERATE_3 = new DecelerateInterpolator(3f);
    public static final Interpolator CUSTOM_40_40 = new PathInterpolator(0.4f, 0f, 0.6f, 1f);
    public static final Interpolator ICON_OVERSHOT = new PathInterpolator(0.4f, 0f, 0.2f, 1.4f);
    public static final Interpolator ICON_OVERSHOT_LESS = new PathInterpolator(0.4f, 0f, 0.2f,
@@ -162,12 +199,77 @@ public class Interpolators {
    public static final Interpolator TOUCH_RESPONSE_REVERSE =
            new PathInterpolator(0.9f, 0f, 0.7f, 1f);

    public static final Interpolator TOUCH_RESPONSE_ACCEL_DEACCEL =
            v -> ACCELERATE_DECELERATE.getInterpolation(TOUCH_RESPONSE.getInterpolation(v));


    /**
     * Inversion of ZOOM_OUT, compounded with an ease-out.
     */
    public static final Interpolator ZOOM_IN = new Interpolator() {
        @Override
        public float getInterpolation(float v) {
            return DECELERATE_3.getInterpolation(1 - ZOOM_OUT.getInterpolation(1 - v));
        }
    };

    public static final Interpolator ZOOM_OUT = new Interpolator() {

        private static final float FOCAL_LENGTH = 0.35f;

        @Override
        public float getInterpolation(float v) {
            return zInterpolate(v);
        }

        /**
         * This interpolator emulates the rate at which the perceived scale of an object changes
         * as its distance from a camera increases. When this interpolator is applied to a scale
         * animation on a view, it evokes the sense that the object is shrinking due to moving away
         * from the camera.
         */
        private float zInterpolate(float input) {
            return (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + input)) /
                    (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + 1.0f));
        }
    };

    public static final Interpolator SCROLL = new Interpolator() {
        @Override
        public float getInterpolation(float t) {
            t -= 1.0f;
            return t*t*t*t*t + 1;
        }
    };

    public static final Interpolator SCROLL_CUBIC = new Interpolator() {
        @Override
        public float getInterpolation(float t) {
            t -= 1.0f;
            return t*t*t + 1;
        }
    };

    private static final float FAST_FLING_PX_MS = 10;

    /*
     * ============================================================================================
     * Functions / Utilities
     * ============================================================================================
     */

    public static Interpolator scrollInterpolatorForVelocity(float velocity) {
        return Math.abs(velocity) > FAST_FLING_PX_MS ? SCROLL : SCROLL_CUBIC;
    }

    /**
     * Create an OvershootInterpolator with tension directly related to the velocity (in px/ms).
     * @param velocity The start velocity of the animation we want to overshoot.
     */
    public static Interpolator overshootInterpolatorForVelocity(float velocity) {
        return new OvershootInterpolator(Math.min(Math.abs(velocity), 3f));
    }

    /**
     * Calculate the amount of overshoot using an exponential falloff function with desired
     * properties, where the overshoot smoothly transitions at the 1.0f boundary into the
@@ -208,4 +310,78 @@ public class Interpolators {
        path.cubicTo(0.208333f, 0.82f, 0.25f, 1f, 1f, 1f);
        return new PathInterpolator(path);
    }

    /**
     * Returns a function that runs the given interpolator such that the entire progress is set
     * between the given bounds. That is, we set the interpolation to 0 until lowerBound and reach
     * 1 by upperBound.
     */
    public static Interpolator clampToProgress(Interpolator interpolator, float lowerBound,
            float upperBound) {
        if (upperBound < lowerBound) {
            throw new IllegalArgumentException(
                    String.format("upperBound (%f) must be greater than lowerBound (%f)",
                            upperBound, lowerBound));
        }
        return t -> clampToProgress(interpolator, t, lowerBound, upperBound);
    }

    /**
     * Returns the progress value's progress between the lower and upper bounds. That is, the
     * progress will be 0f from 0f to lowerBound, and reach 1f by upperBound.
     *
     * Between lowerBound and upperBound, the progress value will be interpolated using the provided
     * interpolator.
     */
    public static float clampToProgress(
            Interpolator interpolator, float progress, float lowerBound, float upperBound) {
        if (upperBound < lowerBound) {
            throw new IllegalArgumentException(
                    String.format("upperBound (%f) must be greater than lowerBound (%f)",
                            upperBound, lowerBound));
        }

        if (progress == lowerBound && progress == upperBound) {
            return progress == 0f ? 0 : 1;
        }
        if (progress < lowerBound) {
            return 0;
        }
        if (progress > upperBound) {
            return 1;
        }
        return interpolator.getInterpolation((progress - lowerBound) / (upperBound - lowerBound));
    }

    /**
     * Returns the progress value's progress between the lower and upper bounds. That is, the
     * progress will be 0f from 0f to lowerBound, and reach 1f by upperBound.
     */
    public static float clampToProgress(float progress, float lowerBound, float upperBound) {
        return clampToProgress(Interpolators.LINEAR, progress, lowerBound, upperBound);
    }

    private static float mapRange(float value, float min, float max) {
        return min + (value * (max - min));
    }

    /**
     * Runs the given interpolator such that the interpolated value is mapped to the given range.
     * This is useful, for example, if we only use this interpolator for part of the animation,
     * such as to take over a user-controlled animation when they let go.
     */
    public static Interpolator mapToProgress(Interpolator interpolator, float lowerBound,
            float upperBound) {
        return t -> mapRange(interpolator.getInterpolation(t), lowerBound, upperBound);
    }

    /**
     * Returns the reverse of the provided interpolator, following the formula: g(x) = 1 - f(1 - x).
     * In practice, this means that if f is an interpolator used to model a value animating between
     * m and n, g is the interpolator to use to obtain the specular behavior when animating from n
     * to m.
     */
    public static Interpolator reverse(Interpolator interpolator) {
        return t -> 1 - interpolator.getInterpolation(1 - t);
    }
}
 No newline at end of file
+175 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import androidx.core.animation.BounceInterpolator;
import androidx.core.animation.DecelerateInterpolator;
import androidx.core.animation.Interpolator;
import androidx.core.animation.LinearInterpolator;
import androidx.core.animation.OvershootInterpolator;
import androidx.core.animation.PathInterpolator;

/**
@@ -65,6 +66,25 @@ public class InterpolatorsAndroidX {
    public static final Interpolator EMPHASIZED_DECELERATE = new PathInterpolator(
            0.05f, 0.7f, 0.1f, 1f);

    public static final Interpolator EXAGGERATED_EASE;
    static {
        Path exaggeratedEase = new Path();
        exaggeratedEase.moveTo(0, 0);
        exaggeratedEase.cubicTo(0.05f, 0f, 0.133333f, 0.08f, 0.166666f, 0.4f);
        exaggeratedEase.cubicTo(0.225f, 0.94f, 0.5f, 1f, 1f, 1f);
        EXAGGERATED_EASE = new PathInterpolator(exaggeratedEase);
    }

    public static final Interpolator INSTANT = t -> 1;
    /**
     * All values of t map to 0 until t == 1. This is primarily useful for setting view visibility,
     * which should only happen at the very end of the animation (when it's already hidden).
     */
    public static final Interpolator FINAL_FRAME = t -> t < 1 ? 0 : 1;

    public static final Interpolator OVERSHOOT_0_75 = new OvershootInterpolator(0.75f);
    public static final Interpolator OVERSHOOT_1_2 = new OvershootInterpolator(1.2f);
    public static final Interpolator OVERSHOOT_1_7 = new OvershootInterpolator(1.7f);

    /*
     * ============================================================================================
@@ -138,11 +158,27 @@ public class InterpolatorsAndroidX {
    public static final Interpolator FAST_OUT_SLOW_IN_REVERSE =
            new PathInterpolator(0.8f, 0f, 0.6f, 1f);
    public static final Interpolator SLOW_OUT_LINEAR_IN = new PathInterpolator(0.8f, 0f, 1f, 1f);
    public static final Interpolator AGGRESSIVE_EASE = new PathInterpolator(0.2f, 0f, 0f, 1f);
    public static final Interpolator AGGRESSIVE_EASE_IN_OUT = new PathInterpolator(0.6f,0, 0.4f, 1);

    public static final Interpolator DECELERATED_EASE = new PathInterpolator(0, 0, .2f, 1f);
    public static final Interpolator ACCELERATED_EASE = new PathInterpolator(0.4f, 0, 1f, 1f);
    public static final Interpolator PREDICTIVE_BACK_DECELERATED_EASE =
            new PathInterpolator(0, 0, 0, 1f);
    public static final Interpolator ALPHA_IN = new PathInterpolator(0.4f, 0f, 1f, 1f);
    public static final Interpolator ALPHA_OUT = new PathInterpolator(0f, 0f, 0.8f, 1f);
    public static final Interpolator ACCELERATE = new AccelerateInterpolator();
    public static final Interpolator ACCELERATE_0_5 = new AccelerateInterpolator(0.5f);
    public static final Interpolator ACCELERATE_0_75 = new AccelerateInterpolator(0.75f);
    public static final Interpolator ACCELERATE_1_5 = new AccelerateInterpolator(1.5f);
    public static final Interpolator ACCELERATE_2 = new AccelerateInterpolator(2);
    public static final Interpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
    public static final Interpolator DECELERATE = new DecelerateInterpolator();
    public static final Interpolator DECELERATE_1_5 = new DecelerateInterpolator(1.5f);
    public static final Interpolator DECELERATE_1_7 = new DecelerateInterpolator(1.7f);
    public static final Interpolator DECELERATE_2 = new DecelerateInterpolator(2);
    public static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f);
    public static final Interpolator DECELERATE_3 = new DecelerateInterpolator(3f);
    public static final Interpolator CUSTOM_40_40 = new PathInterpolator(0.4f, 0f, 0.6f, 1f);
    public static final Interpolator ICON_OVERSHOT = new PathInterpolator(0.4f, 0f, 0.2f, 1.4f);
    public static final Interpolator ICON_OVERSHOT_LESS = new PathInterpolator(0.4f, 0f, 0.2f,
@@ -169,12 +205,77 @@ public class InterpolatorsAndroidX {
    public static final Interpolator TOUCH_RESPONSE_REVERSE =
            new PathInterpolator(0.9f, 0f, 0.7f, 1f);

    public static final Interpolator TOUCH_RESPONSE_ACCEL_DEACCEL =
            v -> ACCELERATE_DECELERATE.getInterpolation(TOUCH_RESPONSE.getInterpolation(v));


    /**
     * Inversion of ZOOM_OUT, compounded with an ease-out.
     */
    public static final Interpolator ZOOM_IN = new Interpolator() {
        @Override
        public float getInterpolation(float v) {
            return DECELERATE_3.getInterpolation(1 - ZOOM_OUT.getInterpolation(1 - v));
        }
    };

    public static final Interpolator ZOOM_OUT = new Interpolator() {

        private static final float FOCAL_LENGTH = 0.35f;

        @Override
        public float getInterpolation(float v) {
            return zInterpolate(v);
        }

        /**
         * This interpolator emulates the rate at which the perceived scale of an object changes
         * as its distance from a camera increases. When this interpolator is applied to a scale
         * animation on a view, it evokes the sense that the object is shrinking due to moving away
         * from the camera.
         */
        private float zInterpolate(float input) {
            return (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + input)) /
                    (1.0f - FOCAL_LENGTH / (FOCAL_LENGTH + 1.0f));
        }
    };

    public static final Interpolator SCROLL = new Interpolator() {
        @Override
        public float getInterpolation(float t) {
            t -= 1.0f;
            return t*t*t*t*t + 1;
        }
    };

    public static final Interpolator SCROLL_CUBIC = new Interpolator() {
        @Override
        public float getInterpolation(float t) {
            t -= 1.0f;
            return t*t*t + 1;
        }
    };

    private static final float FAST_FLING_PX_MS = 10;

    /*
     * ============================================================================================
     * Functions / Utilities
     * ============================================================================================
     */

    public static Interpolator scrollInterpolatorForVelocity(float velocity) {
        return Math.abs(velocity) > FAST_FLING_PX_MS ? SCROLL : SCROLL_CUBIC;
    }

    /**
     * Create an OvershootInterpolator with tension directly related to the velocity (in px/ms).
     * @param velocity The start velocity of the animation we want to overshoot.
     */
    public static Interpolator overshootInterpolatorForVelocity(float velocity) {
        return new OvershootInterpolator(Math.min(Math.abs(velocity), 3f));
    }

    /**
     * Calculate the amount of overshoot using an exponential falloff function with desired
     * properties, where the overshoot smoothly transitions at the 1.0f boundary into the
@@ -215,4 +316,78 @@ public class InterpolatorsAndroidX {
        path.cubicTo(0.208333f, 0.82f, 0.25f, 1f, 1f, 1f);
        return new PathInterpolator(path);
    }

    /**
     * Returns a function that runs the given interpolator such that the entire progress is set
     * between the given bounds. That is, we set the interpolation to 0 until lowerBound and reach
     * 1 by upperBound.
     */
    public static Interpolator clampToProgress(Interpolator interpolator, float lowerBound,
            float upperBound) {
        if (upperBound < lowerBound) {
            throw new IllegalArgumentException(
                    String.format("upperBound (%f) must be greater than lowerBound (%f)",
                            upperBound, lowerBound));
        }
        return t -> clampToProgress(interpolator, t, lowerBound, upperBound);
    }

    /**
     * Returns the progress value's progress between the lower and upper bounds. That is, the
     * progress will be 0f from 0f to lowerBound, and reach 1f by upperBound.
     *
     * Between lowerBound and upperBound, the progress value will be interpolated using the provided
     * interpolator.
     */
    public static float clampToProgress(
            Interpolator interpolator, float progress, float lowerBound, float upperBound) {
        if (upperBound < lowerBound) {
            throw new IllegalArgumentException(
                    String.format("upperBound (%f) must be greater than lowerBound (%f)",
                            upperBound, lowerBound));
        }

        if (progress == lowerBound && progress == upperBound) {
            return progress == 0f ? 0 : 1;
        }
        if (progress < lowerBound) {
            return 0;
        }
        if (progress > upperBound) {
            return 1;
        }
        return interpolator.getInterpolation((progress - lowerBound) / (upperBound - lowerBound));
    }

    /**
     * Returns the progress value's progress between the lower and upper bounds. That is, the
     * progress will be 0f from 0f to lowerBound, and reach 1f by upperBound.
     */
    public static float clampToProgress(float progress, float lowerBound, float upperBound) {
        return clampToProgress(LINEAR, progress, lowerBound, upperBound);
    }

    private static float mapRange(float value, float min, float max) {
        return min + (value * (max - min));
    }

    /**
     * Runs the given interpolator such that the interpolated value is mapped to the given range.
     * This is useful, for example, if we only use this interpolator for part of the animation,
     * such as to take over a user-controlled animation when they let go.
     */
    public static Interpolator mapToProgress(Interpolator interpolator, float lowerBound,
            float upperBound) {
        return t -> mapRange(interpolator.getInterpolation(t), lowerBound, upperBound);
    }

    /**
     * Returns the reverse of the provided interpolator, following the formula: g(x) = 1 - f(1 - x).
     * In practice, this means that if f is an interpolator used to model a value animating between
     * m and n, g is the interpolator to use to obtain the specular behavior when animating from n
     * to m.
     */
    public static Interpolator reverse(Interpolator interpolator) {
        return t -> 1 - interpolator.getInterpolation(1 - t);
    }
}
 No newline at end of file
+6 −1
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

private const val ANDROIDX_ANIM_PACKAGE_NAME = "androidx.core.animation."
private const val ANDROID_ANIM_PACKAGE_NAME = "android.view.animation."

@SmallTest
@RunWith(JUnit4::class)
class InterpolatorsAndroidXTest {
@@ -46,7 +49,9 @@ class InterpolatorsAndroidXTest {
    private fun <T> Class<T>.getPublicMethods() =
            declaredMethods
                    .filter { Modifier.isPublic(it.modifiers) }
                    .map { it.toString().replace(name, "") }
                    .map { it.toString().replace(name, "")
                        .replace(ANDROIDX_ANIM_PACKAGE_NAME, "")
                        .replace(ANDROID_ANIM_PACKAGE_NAME, "") }
                    .toSet()

    private fun <T> Class<T>.getPublicFields() =