blob: 11837b5ca01351f8271fe7dc03c1a0ee2c36f106 [file] [log] [blame]
Chet Haase7c608f22010-10-22 17:54:04 -07001/*
2 * Copyright (C) 2010 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
17package android.animation;
18
19import android.animation.Keyframe.FloatKeyframe;
20
Yigit Boyard422dc32014-09-25 12:23:35 -070021import java.util.List;
Chet Haase7c608f22010-10-22 17:54:04 -070022
23/**
24 * This class holds a collection of FloatKeyframe objects and is called by ValueAnimator to calculate
25 * values between those keyframes for a given animation. The class internal to the animation
26 * package because it is an implementation detail of how Keyframes are stored and used.
27 *
Chet Haaseb2ab04f2011-01-16 11:03:22 -080028 * <p>This type-specific subclass of KeyframeSet, along with the other type-specific subclass for
29 * int, exists to speed up the getValue() method when there is no custom
Chet Haase7c608f22010-10-22 17:54:04 -070030 * TypeEvaluator set for the animation, so that values can be calculated without autoboxing to the
31 * Object equivalents of these primitive types.</p>
32 */
George Mount984011f2014-08-21 14:28:01 -070033class FloatKeyframeSet extends KeyframeSet implements Keyframes.FloatKeyframes {
Chet Haase7c608f22010-10-22 17:54:04 -070034 public FloatKeyframeSet(FloatKeyframe... keyframes) {
35 super(keyframes);
36 }
37
38 @Override
39 public Object getValue(float fraction) {
40 return getFloatValue(fraction);
41 }
42
43 @Override
44 public FloatKeyframeSet clone() {
Yigit Boyard422dc32014-09-25 12:23:35 -070045 final List<Keyframe> keyframes = mKeyframes;
46 final int numKeyframes = mKeyframes.size();
Chet Haase7c608f22010-10-22 17:54:04 -070047 FloatKeyframe[] newKeyframes = new FloatKeyframe[numKeyframes];
48 for (int i = 0; i < numKeyframes; ++i) {
49 newKeyframes[i] = (FloatKeyframe) keyframes.get(i).clone();
50 }
51 FloatKeyframeSet newSet = new FloatKeyframeSet(newKeyframes);
52 return newSet;
53 }
54
Yigit Boyar8619f482014-07-15 17:28:07 -070055 @Override
Chet Haase7c608f22010-10-22 17:54:04 -070056 public float getFloatValue(float fraction) {
Chet Haase7c608f22010-10-22 17:54:04 -070057 if (fraction <= 0f) {
58 final FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(0);
59 final FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(1);
60 float prevValue = prevKeyframe.getFloatValue();
61 float nextValue = nextKeyframe.getFloatValue();
62 float prevFraction = prevKeyframe.getFraction();
63 float nextFraction = nextKeyframe.getFraction();
64 final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
65 if (interpolator != null) {
66 fraction = interpolator.getInterpolation(fraction);
67 }
68 float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction);
69 return mEvaluator == null ?
Chet Haase750e12e2011-02-11 15:21:35 -080070 prevValue + intervalFraction * (nextValue - prevValue) :
Chet Haase7c608f22010-10-22 17:54:04 -070071 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
72 floatValue();
73 } else if (fraction >= 1f) {
74 final FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(mNumKeyframes - 2);
75 final FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(mNumKeyframes - 1);
76 float prevValue = prevKeyframe.getFloatValue();
77 float nextValue = nextKeyframe.getFloatValue();
78 float prevFraction = prevKeyframe.getFraction();
79 float nextFraction = nextKeyframe.getFraction();
80 final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
81 if (interpolator != null) {
82 fraction = interpolator.getInterpolation(fraction);
83 }
84 float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction);
85 return mEvaluator == null ?
Chet Haase750e12e2011-02-11 15:21:35 -080086 prevValue + intervalFraction * (nextValue - prevValue) :
Chet Haase7c608f22010-10-22 17:54:04 -070087 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
88 floatValue();
89 }
90 FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(0);
91 for (int i = 1; i < mNumKeyframes; ++i) {
92 FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(i);
93 if (fraction < nextKeyframe.getFraction()) {
94 final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
Chet Haase7c608f22010-10-22 17:54:04 -070095 float intervalFraction = (fraction - prevKeyframe.getFraction()) /
96 (nextKeyframe.getFraction() - prevKeyframe.getFraction());
97 float prevValue = prevKeyframe.getFloatValue();
98 float nextValue = nextKeyframe.getFloatValue();
Doris Liu6df99052015-03-30 16:03:59 -070099 // Apply interpolator on the proportional duration.
100 if (interpolator != null) {
101 intervalFraction = interpolator.getInterpolation(intervalFraction);
102 }
Chet Haase7c608f22010-10-22 17:54:04 -0700103 return mEvaluator == null ?
Chet Haase750e12e2011-02-11 15:21:35 -0800104 prevValue + intervalFraction * (nextValue - prevValue) :
Chet Haase7c608f22010-10-22 17:54:04 -0700105 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
106 floatValue();
107 }
108 prevKeyframe = nextKeyframe;
109 }
110 // shouldn't get here
111 return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).floatValue();
112 }
113
George Mount984011f2014-08-21 14:28:01 -0700114 @Override
115 public Class getType() {
116 return Float.class;
117 }
Chet Haase7c608f22010-10-22 17:54:04 -0700118}
119