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>int[]</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 IntArrayEvaluator implements TypeEvaluator<int[]> {
264eed52944c0fcb3afa7369aba60fb5c655580286George Mount
274eed52944c0fcb3afa7369aba60fb5c655580286George Mount    private int[] mArray;
284eed52944c0fcb3afa7369aba60fb5c655580286George Mount
294eed52944c0fcb3afa7369aba60fb5c655580286George Mount    /**
304eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * Create an IntArrayEvaluator that does not reuse the animated value. Care must be taken
314eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * when using this option because on every evaluation a new <code>int[]</code> will be
324eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * allocated.
334eed52944c0fcb3afa7369aba60fb5c655580286George Mount     *
344eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @see #IntArrayEvaluator(int[])
354eed52944c0fcb3afa7369aba60fb5c655580286George Mount     */
364eed52944c0fcb3afa7369aba60fb5c655580286George Mount    public IntArrayEvaluator() {
374eed52944c0fcb3afa7369aba60fb5c655580286George Mount    }
384eed52944c0fcb3afa7369aba60fb5c655580286George Mount
394eed52944c0fcb3afa7369aba60fb5c655580286George Mount    /**
404eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * Create an IntArrayEvaluator 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 IntArrayEvaluator(int[] reuseArray) {
484eed52944c0fcb3afa7369aba60fb5c655580286George Mount        mArray = reuseArray;
494eed52944c0fcb3afa7369aba60fb5c655580286George Mount    }
504eed52944c0fcb3afa7369aba60fb5c655580286George Mount
514eed52944c0fcb3afa7369aba60fb5c655580286George Mount    /**
524eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * Interpolates the value at each index by the fraction. If {@link #IntArrayEvaluator(int[])}
534eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * was used to construct this object, <code>reuseArray</code> will be returned, otherwise
544eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * a new <code>int[]</code> will be returned.
554eed52944c0fcb3afa7369aba60fb5c655580286George Mount     *
564eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @param fraction   The fraction from the starting to the ending values
574eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @param startValue The start value.
584eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @param endValue   The end value.
594eed52944c0fcb3afa7369aba60fb5c655580286George Mount     * @return An <code>int[]</code> where each element is an interpolation between
604eed52944c0fcb3afa7369aba60fb5c655580286George Mount     *         the same index in startValue and endValue.
614eed52944c0fcb3afa7369aba60fb5c655580286George Mount     */
624eed52944c0fcb3afa7369aba60fb5c655580286George Mount    @Override
634eed52944c0fcb3afa7369aba60fb5c655580286George Mount    public int[] evaluate(float fraction, int[] startValue, int[] endValue) {
644eed52944c0fcb3afa7369aba60fb5c655580286George Mount        int[] array = mArray;
654eed52944c0fcb3afa7369aba60fb5c655580286George Mount        if (array == null) {
664eed52944c0fcb3afa7369aba60fb5c655580286George Mount            array = new int[startValue.length];
674eed52944c0fcb3afa7369aba60fb5c655580286George Mount        }
684eed52944c0fcb3afa7369aba60fb5c655580286George Mount        for (int i = 0; i < array.length; i++) {
694eed52944c0fcb3afa7369aba60fb5c655580286George Mount            int start = startValue[i];
704eed52944c0fcb3afa7369aba60fb5c655580286George Mount            int end = endValue[i];
714eed52944c0fcb3afa7369aba60fb5c655580286George Mount            array[i] = (int) (start + (fraction * (end - start)));
724eed52944c0fcb3afa7369aba60fb5c655580286George Mount        }
734eed52944c0fcb3afa7369aba60fb5c655580286George Mount        return array;
744eed52944c0fcb3afa7369aba60fb5c655580286George Mount    }
754eed52944c0fcb3afa7369aba60fb5c655580286George Mount}
76