1b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase/*
2b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * Copyright (C) 2013 The Android Open Source Project
3b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase *
4b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
5b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * you may not use this file except in compliance with the License.
6b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * You may obtain a copy of the License at
7b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase *
8b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
9b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase *
10b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * Unless required by applicable law or agreed to in writing, software
11b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
12b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * See the License for the specific language governing permissions and
14b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase * limitations under the License.
15b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase */
16b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haasepackage android.animation;
17b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase
18b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haaseimport android.graphics.Rect;
19b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase
20b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase/**
21edf6f4b49f6e77c349f5055372ce381b74f12efbChet Haase * This evaluator can be used to perform type interpolation between <code>Rect</code> values.
22b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase */
23b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haasepublic class RectEvaluator implements TypeEvaluator<Rect> {
24b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase
25b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase    /**
260f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * When null, a new Rect is returned on every evaluate call. When non-null,
270f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * mRect will be modified and returned on every evaluate.
280f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     */
290f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount    private Rect mRect;
300f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount
310f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount    /**
320f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * Construct a RectEvaluator that returns a new Rect on every evaluate call.
330f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * To avoid creating an object for each evaluate call,
340f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * {@link RectEvaluator#RectEvaluator(android.graphics.Rect)} should be used
350f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * whenever possible.
360f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     */
370f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount    public RectEvaluator() {
380f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount    }
390f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount
400f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount    /**
410f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * Constructs a RectEvaluator that modifies and returns <code>reuseRect</code>
420f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * in {@link #evaluate(float, android.graphics.Rect, android.graphics.Rect)} calls.
430f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * The value returned from
440f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * {@link #evaluate(float, android.graphics.Rect, android.graphics.Rect)} should
450f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * not be cached because it will change over time as the object is reused on each
460f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * call.
470f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     *
480f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * @param reuseRect A Rect to be modified and returned by evaluate.
490f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     */
500f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount    public RectEvaluator(Rect reuseRect) {
510f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount        mRect = reuseRect;
520f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount    }
530f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount
540f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount    /**
55b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * This function returns the result of linearly interpolating the start and
56b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * end Rect values, with <code>fraction</code> representing the proportion
57b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * between the start and end values. The calculation is a simple parametric
58b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * calculation on each of the separate components in the Rect objects
59b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * (left, top, right, and bottom).
60b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     *
610f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * <p>If {@link #RectEvaluator(android.graphics.Rect)} was used to construct
620f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * this RectEvaluator, the object returned will be the <code>reuseRect</code>
630f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     * passed into the constructor.</p>
640f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount     *
65b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * @param fraction   The fraction from the starting to the ending values
66b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * @param startValue The start Rect
67b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * @param endValue   The end Rect
68b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     * @return A linear interpolation between the start and end values, given the
69b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     *         <code>fraction</code> parameter.
70b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase     */
71b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase    @Override
72b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase    public Rect evaluate(float fraction, Rect startValue, Rect endValue) {
730f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount        int left = startValue.left + (int) ((endValue.left - startValue.left) * fraction);
740f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount        int top = startValue.top + (int) ((endValue.top - startValue.top) * fraction);
750f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount        int right = startValue.right + (int) ((endValue.right - startValue.right) * fraction);
760f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount        int bottom = startValue.bottom + (int) ((endValue.bottom - startValue.bottom) * fraction);
770f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount        if (mRect == null) {
780f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount            return new Rect(left, top, right, bottom);
790f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount        } else {
800f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount            mRect.set(left, top, right, bottom);
810f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount            return mRect;
820f3f2983db5c5f7aa2395408165b326c420000ddGeorge Mount        }
83b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase    }
84b989502e5cf44d65c6dddc0179b6d9b6e61ef7fdChet Haase}
85