14eed52944c0fcb3afa7369aba60fb5c655580286George Mount/*
24eed52944c0fcb3afa7369aba60fb5c655580286George Mount * Copyright (C) 2013 The Android Open Source Project
34eed52944c0fcb3afa7369aba60fb5c655580286George Mount *
44eed52944c0fcb3afa7369aba60fb5c655580286George Mount * Licensed under the Apache License, Version 2.0 (the "License");
54eed52944c0fcb3afa7369aba60fb5c655580286George Mount * you may not use this file except in compliance with the License.
64eed52944c0fcb3afa7369aba60fb5c655580286George Mount * You may obtain a copy of the License at
74eed52944c0fcb3afa7369aba60fb5c655580286George Mount *
84eed52944c0fcb3afa7369aba60fb5c655580286George Mount *      http://www.apache.org/licenses/LICENSE-2.0
94eed52944c0fcb3afa7369aba60fb5c655580286George Mount *
104eed52944c0fcb3afa7369aba60fb5c655580286George Mount * Unless required by applicable law or agreed to in writing, software
114eed52944c0fcb3afa7369aba60fb5c655580286George Mount * distributed under the License is distributed on an "AS IS" BASIS,
124eed52944c0fcb3afa7369aba60fb5c655580286George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134eed52944c0fcb3afa7369aba60fb5c655580286George Mount * See the License for the specific language governing permissions and
144eed52944c0fcb3afa7369aba60fb5c655580286George Mount * limitations under the License.
154eed52944c0fcb3afa7369aba60fb5c655580286George Mount */
164eed52944c0fcb3afa7369aba60fb5c655580286George Mount
174eed52944c0fcb3afa7369aba60fb5c655580286George Mountpackage android.animation;
184eed52944c0fcb3afa7369aba60fb5c655580286George Mount
194eed52944c0fcb3afa7369aba60fb5c655580286George Mount/**
204eed52944c0fcb3afa7369aba60fb5c655580286George Mount * This evaluator can be used to perform type interpolation between <code>float[]</code> values.
214eed52944c0fcb3afa7369aba60fb5c655580286George Mount * Each index into the array is treated as a separate value to interpolate. For example,
224eed52944c0fcb3afa7369aba60fb5c655580286George Mount * evaluating <code>{100, 200}</code> and <code>{300, 400}</code> will interpolate the value at
234eed52944c0fcb3afa7369aba60fb5c655580286George Mount * the first index between 100 and 300 and the value at the second index value between 200 and 400.
244eed52944c0fcb3afa7369aba60fb5c655580286George Mount */
254eed52944c0fcb3afa7369aba60fb5c655580286George Mountpublic class FloatArrayEvaluator implements TypeEvaluator<float[]> {
264eed52944c0fcb3afa7369aba60fb5c655580286George Mount
274eed52944c0fcb3afa7369aba60fb5c655580286George Mount    private float[] mArray;
284eed52944c0fcb3afa7369aba60fb5c655580286George Mount
294eed52944c0fcb3afa7369aba60fb5c655580286George Mount    /**
304eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * Create a FloatArrayEvaluator that does not reuse the animated value. Care must be taken
314eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * when using this option because on every evaluation a new <code>float[]</code> will be
324eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * allocated.
334eed52944c0fcb3afa7369aba60fb5c655580286George Mount     *
344eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @see #FloatArrayEvaluator(float[])
354eed52944c0fcb3afa7369aba60fb5c655580286George Mount     */
364eed52944c0fcb3afa7369aba60fb5c655580286George Mount    public FloatArrayEvaluator() {
374eed52944c0fcb3afa7369aba60fb5c655580286George Mount    }
384eed52944c0fcb3afa7369aba60fb5c655580286George Mount
394eed52944c0fcb3afa7369aba60fb5c655580286George Mount    /**
404eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * Create a FloatArrayEvaluator that reuses <code>reuseArray</code> for every evaluate() call.
414eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * Caution must be taken to ensure that the value returned from
424eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * {@link android.animation.ValueAnimator#getAnimatedValue()} is not cached, modified, or
434eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * used across threads. The value will be modified on each <code>evaluate()</code> call.
444eed52944c0fcb3afa7369aba60fb5c655580286George Mount     *
454eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @param reuseArray The array to modify and return from <code>evaluate</code>.
464eed52944c0fcb3afa7369aba60fb5c655580286George Mount     */
474eed52944c0fcb3afa7369aba60fb5c655580286George Mount    public FloatArrayEvaluator(float[] reuseArray) {
484eed52944c0fcb3afa7369aba60fb5c655580286George Mount        mArray = reuseArray;
494eed52944c0fcb3afa7369aba60fb5c655580286George Mount    }
504eed52944c0fcb3afa7369aba60fb5c655580286George Mount
514eed52944c0fcb3afa7369aba60fb5c655580286George Mount    /**
524eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * Interpolates the value at each index by the fraction. If
534eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * {@link #FloatArrayEvaluator(float[])} was used to construct this object,
544eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * <code>reuseArray</code> will be returned, otherwise a new <code>float[]</code>
554eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * will be returned.
564eed52944c0fcb3afa7369aba60fb5c655580286George Mount     *
574eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @param fraction   The fraction from the starting to the ending values
584eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @param startValue The start value.
594eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @param endValue   The end value.
604eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @return A <code>float[]</code> where each element is an interpolation between
614eed52944c0fcb3afa7369aba60fb5c655580286George Mount     *         the same index in startValue and endValue.
624eed52944c0fcb3afa7369aba60fb5c655580286George Mount     */
634eed52944c0fcb3afa7369aba60fb5c655580286George Mount    @Override
644eed52944c0fcb3afa7369aba60fb5c655580286George Mount    public float[] evaluate(float fraction, float[] startValue, float[] endValue) {
654eed52944c0fcb3afa7369aba60fb5c655580286George Mount        float[] array = mArray;
664eed52944c0fcb3afa7369aba60fb5c655580286George Mount        if (array == null) {
674eed52944c0fcb3afa7369aba60fb5c655580286George Mount            array = new float[startValue.length];
684eed52944c0fcb3afa7369aba60fb5c655580286George Mount        }
694eed52944c0fcb3afa7369aba60fb5c655580286George Mount
704eed52944c0fcb3afa7369aba60fb5c655580286George Mount        for (int i = 0; i < array.length; i++) {
714eed52944c0fcb3afa7369aba60fb5c655580286George Mount            float start = startValue[i];
724eed52944c0fcb3afa7369aba60fb5c655580286George Mount            float end = endValue[i];
734eed52944c0fcb3afa7369aba60fb5c655580286George Mount            array[i] = start + (fraction * (end - start));
744eed52944c0fcb3afa7369aba60fb5c655580286George Mount        }
754eed52944c0fcb3afa7369aba60fb5c655580286George Mount        return array;
764eed52944c0fcb3afa7369aba60fb5c655580286George Mount    }
774eed52944c0fcb3afa7369aba60fb5c655580286George Mount}
78