1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package androidx.core.animation;
17 
18 import org.jspecify.annotations.NonNull;
19 
20 import java.util.List;
21 
22 /**
23  * This interface abstracts a collection of Keyframe objects and is called by
24  * ValueAnimator to calculate values between those keyframes for a given animation.
25  */
26 interface Keyframes<T> extends Cloneable {
27 
28     /**
29      * Sets the TypeEvaluator to be used when calculating animated values. This object
30      * is required only for Keyframes that are not either IntKeyframes or FloatKeyframes,
31      * both of which assume their own evaluator to speed up calculations with those primitive
32      * types.
33      *
34      * @param evaluator The TypeEvaluator to be used to calculate animated values.
35      */
setEvaluator(TypeEvaluator<T> evaluator)36     void setEvaluator(TypeEvaluator<T> evaluator);
37 
38     /**
39      * @return The value type contained by the contained Keyframes.
40      */
getType()41     Class<?> getType();
42 
43     /**
44      * Gets the animated value, given the elapsed fraction of the animation (interpolated by the
45      * animation's interpolator) and the evaluator used to calculate in-between values. This
46      * function maps the input fraction to the appropriate keyframe interval and a fraction
47      * between them and returns the interpolated value. Note that the input fraction may fall
48      * outside the [0-1] bounds, if the animation's interpolator made that happen (e.g., a
49      * spring interpolation that might send the fraction past 1.0). We handle this situation by
50      * just using the two keyframes at the appropriate end when the value is outside those bounds.
51      *
52      * @param fraction The elapsed fraction of the animation
53      * @return The animated value.
54      */
getValue(float fraction)55     T getValue(float fraction);
56 
57     /**
58      * @return A list of all Keyframes contained by this. This may return null if this is
59      * not made up of Keyframes.
60      */
getKeyframes()61     List<Keyframe<T>> getKeyframes();
62 
clone()63     @NonNull Keyframes clone();
64 
65     /**
66      * A specialization of Keyframes that has integer primitive value calculation.
67      */
68     public interface IntKeyframes extends Keyframes<Integer> {
69 
70         /**
71          * Works like {@link #getValue(float)}, but returning a primitive.
72          * @param fraction The elapsed fraction of the animation
73          * @return The animated value.
74          */
getIntValue(float fraction)75         int getIntValue(float fraction);
76     }
77 
78     /**
79      * A specialization of Keyframes that has float primitive value calculation.
80      */
81     public interface FloatKeyframes extends Keyframes<Float> {
82 
83         /**
84          * Works like {@link #getValue(float)}, but returning a primitive.
85          * @param fraction The elapsed fraction of the animation
86          * @return The animated value.
87          */
getFloatValue(float fraction)88         float getFloatValue(float fraction);
89     }
90 }
91