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