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