George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.animation; |
| 17 | |
| 18 | import android.graphics.PointF; |
| 19 | |
| 20 | /** |
| 21 | * This evaluator can be used to perform type interpolation between <code>PointF</code> values. |
| 22 | */ |
| 23 | public class PointFEvaluator implements TypeEvaluator<PointF> { |
| 24 | |
| 25 | /** |
| 26 | * When null, a new PointF is returned on every evaluate call. When non-null, |
| 27 | * mPoint will be modified and returned on every evaluate. |
| 28 | */ |
| 29 | private PointF mPoint; |
| 30 | |
| 31 | /** |
| 32 | * Construct a PointFEvaluator that returns a new PointF on every evaluate call. |
| 33 | * To avoid creating an object for each evaluate call, |
| 34 | * {@link PointFEvaluator#PointFEvaluator(android.graphics.PointF)} should be used |
| 35 | * whenever possible. |
| 36 | */ |
| 37 | public PointFEvaluator() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Constructs a PointFEvaluator that modifies and returns <code>reuse</code> |
| 42 | * in {@link #evaluate(float, android.graphics.PointF, android.graphics.PointF)} calls. |
| 43 | * The value returned from |
| 44 | * {@link #evaluate(float, android.graphics.PointF, android.graphics.PointF)} should |
| 45 | * not be cached because it will change over time as the object is reused on each |
| 46 | * call. |
| 47 | * |
| 48 | * @param reuse A PointF to be modified and returned by evaluate. |
| 49 | */ |
| 50 | public PointFEvaluator(PointF reuse) { |
| 51 | mPoint = reuse; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * This function returns the result of linearly interpolating the start and |
| 56 | * end PointF 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 PointF objects |
| 59 | * (x, y). |
| 60 | * |
| 61 | * <p>If {@link #PointFEvaluator(android.graphics.PointF)} was used to construct |
| 62 | * this PointFEvaluator, the object returned will be the <code>reuse</code> |
| 63 | * passed into the constructor.</p> |
| 64 | * |
| 65 | * @param fraction The fraction from the starting to the ending values |
| 66 | * @param startValue The start PointF |
| 67 | * @param endValue The end PointF |
| 68 | * @return A linear interpolation between the start and end values, given the |
| 69 | * <code>fraction</code> parameter. |
| 70 | */ |
| 71 | @Override |
| 72 | public PointF evaluate(float fraction, PointF startValue, PointF endValue) { |
| 73 | float x = startValue.x + (fraction * (endValue.x - startValue.x)); |
| 74 | float y = startValue.y + (fraction * (endValue.y - startValue.y)); |
| 75 | |
| 76 | if (mPoint != null) { |
| 77 | mPoint.set(x, y); |
| 78 | return mPoint; |
| 79 | } else { |
| 80 | return new PointF(x, y); |
| 81 | } |
| 82 | } |
| 83 | } |