From 0f3f2983db5c5f7aa2395408165b326c420000dd Mon Sep 17 00:00:00 2001 From: George Mount Date: Thu, 12 Sep 2013 10:49:55 -0700 Subject: [PATCH] Adds reuse Rect constructor for RectEvaluator. Adds constructor to RectEvaluator that allows reuse of the evaluated Rect Object. Change-Id: I09ca4fd7dadb20d1501248c83dc008c00d901fd6 --- api/current.txt | 3 +- .../java/android/animation/RectEvaluator.java | 47 +++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/api/current.txt b/api/current.txt index e6fb767f04a81..115f0bfcbca81 100644 --- a/api/current.txt +++ b/api/current.txt @@ -2540,6 +2540,7 @@ package android.animation { public class RectEvaluator implements android.animation.TypeEvaluator { ctor public RectEvaluator(); + ctor public RectEvaluator(android.graphics.Rect); method public android.graphics.Rect evaluate(float, android.graphics.Rect, android.graphics.Rect); } @@ -31782,8 +31783,6 @@ package android.widget { ctor public NumberPicker(android.content.Context, android.util.AttributeSet); ctor public NumberPicker(android.content.Context, android.util.AttributeSet, int); ctor public NumberPicker(android.content.Context, android.util.AttributeSet, int, int); - method public int computeVerticalScrollOffset(); - method public int computeVerticalScrollRange(); method public java.lang.String[] getDisplayedValues(); method public int getMaxValue(); method public int getMinValue(); diff --git a/core/java/android/animation/RectEvaluator.java b/core/java/android/animation/RectEvaluator.java index 28d496b346f05..23eb766da62af 100644 --- a/core/java/android/animation/RectEvaluator.java +++ b/core/java/android/animation/RectEvaluator.java @@ -22,6 +22,35 @@ import android.graphics.Rect; */ public class RectEvaluator implements TypeEvaluator { + /** + * When null, a new Rect is returned on every evaluate call. When non-null, + * mRect will be modified and returned on every evaluate. + */ + private Rect mRect; + + /** + * Construct a RectEvaluator that returns a new Rect on every evaluate call. + * To avoid creating an object for each evaluate call, + * {@link RectEvaluator#RectEvaluator(android.graphics.Rect)} should be used + * whenever possible. + */ + public RectEvaluator() { + } + + /** + * Constructs a RectEvaluator that modifies and returns reuseRect + * in {@link #evaluate(float, android.graphics.Rect, android.graphics.Rect)} calls. + * The value returned from + * {@link #evaluate(float, android.graphics.Rect, android.graphics.Rect)} should + * not be cached because it will change over time as the object is reused on each + * call. + * + * @param reuseRect A Rect to be modified and returned by evaluate. + */ + public RectEvaluator(Rect reuseRect) { + mRect = reuseRect; + } + /** * This function returns the result of linearly interpolating the start and * end Rect values, with fraction representing the proportion @@ -29,6 +58,10 @@ public class RectEvaluator implements TypeEvaluator { * calculation on each of the separate components in the Rect objects * (left, top, right, and bottom). * + *

If {@link #RectEvaluator(android.graphics.Rect)} was used to construct + * this RectEvaluator, the object returned will be the reuseRect + * passed into the constructor.

+ * * @param fraction The fraction from the starting to the ending values * @param startValue The start Rect * @param endValue The end Rect @@ -37,9 +70,15 @@ public class RectEvaluator implements TypeEvaluator { */ @Override public Rect evaluate(float fraction, Rect startValue, Rect endValue) { - return new Rect(startValue.left + (int)((endValue.left - startValue.left) * fraction), - startValue.top + (int)((endValue.top - startValue.top) * fraction), - startValue.right + (int)((endValue.right - startValue.right) * fraction), - startValue.bottom + (int)((endValue.bottom - startValue.bottom) * fraction)); + int left = startValue.left + (int) ((endValue.left - startValue.left) * fraction); + int top = startValue.top + (int) ((endValue.top - startValue.top) * fraction); + int right = startValue.right + (int) ((endValue.right - startValue.right) * fraction); + int bottom = startValue.bottom + (int) ((endValue.bottom - startValue.bottom) * fraction); + if (mRect == null) { + return new Rect(left, top, right, bottom); + } else { + mRect.set(left, top, right, bottom); + return mRect; + } } }