1/*
2 * Copyright (C) 2016 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
17package androidx.transition;
18
19import android.animation.TypeEvaluator;
20import android.graphics.Rect;
21
22/**
23 * This evaluator can be used to perform type interpolation between <code>Rect</code> values.
24 */
25class RectEvaluator implements TypeEvaluator<Rect> {
26
27    /**
28     * When null, a new Rect is returned on every evaluate call. When non-null,
29     * mRect will be modified and returned on every evaluate.
30     */
31    private Rect mRect;
32
33    /**
34     * Construct a RectEvaluator that returns a new Rect on every evaluate call.
35     * To avoid creating an object for each evaluate call,
36     * {@link RectEvaluator#RectEvaluator(android.graphics.Rect)} should be used
37     * whenever possible.
38     */
39    RectEvaluator() {
40    }
41
42    /**
43     * Constructs a RectEvaluator that modifies and returns <code>reuseRect</code>
44     * in {@link #evaluate(float, android.graphics.Rect, android.graphics.Rect)} calls.
45     * The value returned from
46     * {@link #evaluate(float, android.graphics.Rect, android.graphics.Rect)} should
47     * not be cached because it will change over time as the object is reused on each
48     * call.
49     *
50     * @param reuseRect A Rect to be modified and returned by evaluate.
51     */
52    RectEvaluator(Rect reuseRect) {
53        mRect = reuseRect;
54    }
55
56    /**
57     * This function returns the result of linearly interpolating the start and
58     * end Rect values, with <code>fraction</code> representing the proportion
59     * between the start and end values. The calculation is a simple parametric
60     * calculation on each of the separate components in the Rect objects
61     * (left, top, right, and bottom).
62     *
63     * <p>If {@link #RectEvaluator(android.graphics.Rect)} was used to construct
64     * this RectEvaluator, the object returned will be the <code>reuseRect</code>
65     * passed into the constructor.</p>
66     *
67     * @param fraction   The fraction from the starting to the ending values
68     * @param startValue The start Rect
69     * @param endValue   The end Rect
70     * @return A linear interpolation between the start and end values, given the
71     * <code>fraction</code> parameter.
72     */
73    @Override
74    public Rect evaluate(float fraction, Rect startValue, Rect endValue) {
75        int left = startValue.left + (int) ((endValue.left - startValue.left) * fraction);
76        int top = startValue.top + (int) ((endValue.top - startValue.top) * fraction);
77        int right = startValue.right + (int) ((endValue.right - startValue.right) * fraction);
78        int bottom = startValue.bottom + (int) ((endValue.bottom - startValue.bottom) * fraction);
79        if (mRect == null) {
80            return new Rect(left, top, right, bottom);
81        } else {
82            mRect.set(left, top, right, bottom);
83            return mRect;
84        }
85    }
86}
87