George Mount | 4eed529 | 2013-08-30 13:56:01 -0700 | [diff] [blame] | 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>float[]</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 FloatArrayEvaluator implements TypeEvaluator<float[]> { |
| 26 | |
| 27 | private float[] mArray; |
| 28 | |
| 29 | /** |
| 30 | * Create a FloatArrayEvaluator that does not reuse the animated value. Care must be taken |
| 31 | * when using this option because on every evaluation a new <code>float[]</code> will be |
| 32 | * allocated. |
| 33 | * |
| 34 | * @see #FloatArrayEvaluator(float[]) |
| 35 | */ |
| 36 | public FloatArrayEvaluator() { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create a FloatArrayEvaluator 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 | */ |
| 47 | public FloatArrayEvaluator(float[] reuseArray) { |
| 48 | mArray = reuseArray; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Interpolates the value at each index by the fraction. If |
| 53 | * {@link #FloatArrayEvaluator(float[])} was used to construct this object, |
| 54 | * <code>reuseArray</code> will be returned, otherwise a new <code>float[]</code> |
| 55 | * will be returned. |
| 56 | * |
| 57 | * @param fraction The fraction from the starting to the ending values |
| 58 | * @param startValue The start value. |
| 59 | * @param endValue The end value. |
| 60 | * @return A <code>float[]</code> where each element is an interpolation between |
| 61 | * the same index in startValue and endValue. |
| 62 | */ |
| 63 | @Override |
| 64 | public float[] evaluate(float fraction, float[] startValue, float[] endValue) { |
| 65 | float[] array = mArray; |
| 66 | if (array == null) { |
| 67 | array = new float[startValue.length]; |
| 68 | } |
| 69 | |
| 70 | for (int i = 0; i < array.length; i++) { |
| 71 | float start = startValue[i]; |
| 72 | float end = endValue[i]; |
| 73 | array[i] = start + (fraction * (end - start)); |
| 74 | } |
| 75 | return array; |
| 76 | } |
| 77 | } |