1/*
2 * Copyright (C) 2017 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.support.transition;
18
19import android.animation.TypeEvaluator;
20
21/**
22 * This evaluator can be used to perform type interpolation between <code>float[]</code> values.
23 * Each index into the array is treated as a separate value to interpolate. For example,
24 * evaluating <code>{100, 200}</code> and <code>{300, 400}</code> will interpolate the value at
25 * the first index between 100 and 300 and the value at the second index value between 200 and 400.
26 */
27class FloatArrayEvaluator implements TypeEvaluator<float[]> {
28
29    private float[] mArray;
30
31    /**
32     * Create a FloatArrayEvaluator that reuses <code>reuseArray</code> for every evaluate() call.
33     * Caution must be taken to ensure that the value returned from
34     * {@link android.animation.ValueAnimator#getAnimatedValue()} is not cached, modified, or
35     * used across threads. The value will be modified on each <code>evaluate()</code> call.
36     *
37     * @param reuseArray The array to modify and return from <code>evaluate</code>.
38     */
39    FloatArrayEvaluator(float[] reuseArray) {
40        mArray = reuseArray;
41    }
42
43    /**
44     * Interpolates the value at each index by the fraction. If
45     * {@link #FloatArrayEvaluator(float[])} was used to construct this object,
46     * <code>reuseArray</code> will be returned, otherwise a new <code>float[]</code>
47     * will be returned.
48     *
49     * @param fraction   The fraction from the starting to the ending values
50     * @param startValue The start value.
51     * @param endValue   The end value.
52     * @return A <code>float[]</code> where each element is an interpolation between
53     * the same index in startValue and endValue.
54     */
55    @Override
56    public float[] evaluate(float fraction, float[] startValue, float[] endValue) {
57        float[] array = mArray;
58        if (array == null) {
59            array = new float[startValue.length];
60        }
61
62        for (int i = 0; i < array.length; i++) {
63            float start = startValue[i];
64            float end = endValue[i];
65            array[i] = start + (fraction * (end - start));
66        }
67        return array;
68    }
69
70}
71