diff options
author | Chet Haase <chet@google.com> | 2013-06-04 10:44:40 -0700 |
---|---|---|
committer | Chet Haase <chet@google.com> | 2013-06-04 10:44:40 -0700 |
commit | 08735185f8105710e18ad02297461bec9268e514 (patch) | |
tree | 71ac76413081b75700134c3006fa4eb921d1a8a6 /core | |
parent | aca7d39d087741f7e88e012db95ad961f9f08955 (diff) | |
download | frameworks_base-08735185f8105710e18ad02297461bec9268e514.zip frameworks_base-08735185f8105710e18ad02297461bec9268e514.tar.gz frameworks_base-08735185f8105710e18ad02297461bec9268e514.tar.bz2 |
Use ArrayMap instead of HashMap in transitions
The new ArrayMap class is more efficient for small collections.
Transitions use maps all over the place to collect/use property
values in setting up the transition animations. Changing to ArrayMap
should be more efficient, especially in terms of memory allocations
and GCs.
Issue #9276256 Transitions: Reduce memory allocations
Change-Id: I07b7d4ba68d5a207808eca11943aa3578fa90e3e
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/util/ArrayMap.java | 10 | ||||
-rw-r--r-- | core/java/android/view/transition/Crossfade.java | 11 | ||||
-rw-r--r-- | core/java/android/view/transition/Move.java | 7 | ||||
-rw-r--r-- | core/java/android/view/transition/Recolor.java | 7 | ||||
-rw-r--r-- | core/java/android/view/transition/TextChange.java | 11 | ||||
-rw-r--r-- | core/java/android/view/transition/Transition.java | 21 | ||||
-rw-r--r-- | core/java/android/view/transition/TransitionGroup.java | 10 | ||||
-rw-r--r-- | core/java/android/view/transition/TransitionInflater.java | 6 | ||||
-rw-r--r-- | core/java/android/view/transition/TransitionManager.java | 14 | ||||
-rw-r--r-- | core/java/android/view/transition/TransitionValues.java | 7 |
10 files changed, 58 insertions, 46 deletions
diff --git a/core/java/android/util/ArrayMap.java b/core/java/android/util/ArrayMap.java index 6da7546..edda779 100644 --- a/core/java/android/util/ArrayMap.java +++ b/core/java/android/util/ArrayMap.java @@ -206,6 +206,16 @@ public final class ArrayMap<K, V> implements Map<K, V> { } mSize = 0; } + /** + * Create a new ArrayMap with the mappings from the given ArrayMap. + */ + public ArrayMap(ArrayMap map) { + this(); + if (map != null) { + putAll(map); + } + } + /** * Make the array map empty. All storage is released. diff --git a/core/java/android/view/transition/Crossfade.java b/core/java/android/view/transition/Crossfade.java index babf58f..7a55b0d 100644 --- a/core/java/android/view/transition/Crossfade.java +++ b/core/java/android/view/transition/Crossfade.java @@ -26,13 +26,14 @@ import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.util.ArrayMap; import android.util.Log; import android.view.SurfaceView; import android.view.TextureView; import android.view.View; import android.view.ViewGroup; -import java.util.HashMap; +import java.util.Map; /** * This transition captures bitmap representations of target views before and @@ -60,8 +61,8 @@ public class Crossfade extends Transition { return false; } final View view = startValues.view; - HashMap<String, Object> startVals = startValues.values; - HashMap<String, Object> endVals = endValues.values; + Map<String, Object> startVals = startValues.values; + Map<String, Object> endVals = endValues.values; Bitmap startBitmap = (Bitmap) startVals.get(PROPNAME_BITMAP); Bitmap endBitmap = (Bitmap) endVals.get(PROPNAME_BITMAP); Drawable startDrawable = (Drawable) startVals.get(PROPNAME_DRAWABLE); @@ -85,8 +86,8 @@ public class Crossfade extends Transition { if (startValues == null || endValues == null) { return null; } - HashMap<String, Object> startVals = startValues.values; - HashMap<String, Object> endVals = endValues.values; + Map<String, Object> startVals = startValues.values; + Map<String, Object> endVals = endValues.values; final View view = endValues.view; Rect startBounds = (Rect) startVals.get(PROPNAME_BOUNDS); diff --git a/core/java/android/view/transition/Move.java b/core/java/android/view/transition/Move.java index 96441aa..1d05419 100644 --- a/core/java/android/view/transition/Move.java +++ b/core/java/android/view/transition/Move.java @@ -24,11 +24,12 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; +import android.util.ArrayMap; import android.util.Log; import android.view.View; import android.view.ViewGroup; -import java.util.HashMap; +import java.util.Map; /** * This transition captures the layout bounds of target views before and after @@ -216,8 +217,8 @@ public class Move extends Transition { if (startValues == null || endValues == null) { return false; } - HashMap<String, Object> startParentVals = startValues.values; - HashMap<String, Object> endParentVals = endValues.values; + Map<String, Object> startParentVals = startValues.values; + Map<String, Object> endParentVals = endValues.values; ViewGroup startParent = (ViewGroup) startParentVals.get(PROPNAME_PARENT); ViewGroup endParent = (ViewGroup) endParentVals.get(PROPNAME_PARENT); if (startParent == null || endParent == null) { diff --git a/core/java/android/view/transition/Recolor.java b/core/java/android/view/transition/Recolor.java index 7048be9..45407e1 100644 --- a/core/java/android/view/transition/Recolor.java +++ b/core/java/android/view/transition/Recolor.java @@ -20,11 +20,12 @@ import android.animation.ArgbEvaluator; import android.animation.ObjectAnimator; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; +import android.util.ArrayMap; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import java.util.HashMap; +import java.util.Map; /** * This transition tracks changes during scene changes to the @@ -86,8 +87,8 @@ public class Recolor extends Transition { } ObjectAnimator anim = null; final View view = endValues.view; - HashMap<String, Object> startVals = startValues.values; - HashMap<String, Object> endVals = endValues.values; + Map<String, Object> startVals = startValues.values; + Map<String, Object> endVals = endValues.values; Drawable startBackground = (Drawable) startVals.get(PROPNAME_BACKGROUND); Drawable endBackground = (Drawable) endVals.get(PROPNAME_BACKGROUND); if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) { diff --git a/core/java/android/view/transition/TextChange.java b/core/java/android/view/transition/TextChange.java index 0ba2412..ac2852c 100644 --- a/core/java/android/view/transition/TextChange.java +++ b/core/java/android/view/transition/TextChange.java @@ -18,10 +18,11 @@ package android.view.transition; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ValueAnimator; +import android.util.ArrayMap; import android.view.ViewGroup; import android.widget.TextView; -import java.util.HashMap; +import java.util.Map; /** * This transition tracks changes to the text in TextView targets. If the text @@ -51,8 +52,8 @@ public class TextChange extends Transition { return false; } final TextView view = (TextView) endValues.view; - HashMap<String, Object> startVals = startValues.values; - HashMap<String, Object> endVals = endValues.values; + Map<String, Object> startVals = startValues.values; + Map<String, Object> endVals = endValues.values; String startText = (String) startVals.get(PROPNAME_TEXT); String endText = (String) endVals.get(PROPNAME_TEXT); if (!startText.equals(endText)) { @@ -69,8 +70,8 @@ public class TextChange extends Transition { return null; } final TextView view = (TextView) endValues.view; - HashMap<String, Object> startVals = startValues.values; - HashMap<String, Object> endVals = endValues.values; + Map<String, Object> startVals = startValues.values; + Map<String, Object> endVals = endValues.values; final String startText = (String) startVals.get(PROPNAME_TEXT); final String endText = (String) endVals.get(PROPNAME_TEXT); if (!startText.equals(endText)) { diff --git a/core/java/android/view/transition/Transition.java b/core/java/android/view/transition/Transition.java index 8f2bc5a..4591942 100644 --- a/core/java/android/view/transition/Transition.java +++ b/core/java/android/view/transition/Transition.java @@ -18,6 +18,7 @@ package android.view.transition; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.TimeInterpolator; +import android.util.ArrayMap; import android.util.LongSparseArray; import android.util.SparseArray; import android.view.SurfaceView; @@ -28,7 +29,6 @@ import android.view.ViewOverlay; import android.widget.ListView; import java.util.ArrayList; -import java.util.HashMap; /** * A Transition holds information about animations that will be run on its @@ -62,14 +62,13 @@ public abstract class Transition { TimeInterpolator mInterpolator = null; int[] mTargetIds; View[] mTargets; - // TODO: sparse arrays instead of hashmaps? - private HashMap<View, TransitionValues> mStartValues = - new HashMap<View, TransitionValues>(); + private ArrayMap<View, TransitionValues> mStartValues = + new ArrayMap<View, TransitionValues>(); private SparseArray<TransitionValues> mStartIdValues = new SparseArray<TransitionValues>(); private LongSparseArray<TransitionValues> mStartItemIdValues = new LongSparseArray<TransitionValues>(); - private HashMap<View, TransitionValues> mEndValues = - new HashMap<View, TransitionValues>(); + private ArrayMap<View, TransitionValues> mEndValues = + new ArrayMap<View, TransitionValues>(); private SparseArray<TransitionValues> mEndIdValues = new SparseArray<TransitionValues>(); private LongSparseArray<TransitionValues> mEndItemIdValues = new LongSparseArray<TransitionValues>(); @@ -227,15 +226,15 @@ public abstract class Transition { * * @hide */ - protected void prePlay(ViewGroup sceneRoot, HashMap<View, TransitionValues> startValues, + protected void prePlay(ViewGroup sceneRoot, ArrayMap<View, TransitionValues> startValues, SparseArray<TransitionValues> startIdValues, LongSparseArray<TransitionValues> startItemIdValues, - HashMap<View, TransitionValues> endValues, + ArrayMap<View, TransitionValues> endValues, SparseArray<TransitionValues> endIdValues, LongSparseArray<TransitionValues> endItemIdValues) { mPlayStartValuesList.clear(); mPlayEndValuesList.clear(); - HashMap<View, TransitionValues> endCopy = new HashMap<View, TransitionValues>(endValues); + ArrayMap<View, TransitionValues> endCopy = new ArrayMap<View, TransitionValues>(endValues); SparseArray<TransitionValues> endIdCopy = new SparseArray<TransitionValues>(endIdValues.size()); for (int i = 0; i < endIdValues.size(); ++i) { @@ -392,10 +391,10 @@ public abstract class Transition { * @hide */ protected void play(ViewGroup sceneRoot, - final HashMap<View, TransitionValues> startValues, + final ArrayMap<View, TransitionValues> startValues, final SparseArray<TransitionValues> startIdValues, final LongSparseArray<TransitionValues> startItemIdValues, - final HashMap<View, TransitionValues> endValues, + final ArrayMap<View, TransitionValues> endValues, final SparseArray<TransitionValues> endIdValues, final LongSparseArray<TransitionValues> endItemIdValues) { diff --git a/core/java/android/view/transition/TransitionGroup.java b/core/java/android/view/transition/TransitionGroup.java index 4ebb53f..8ff46b6 100644 --- a/core/java/android/view/transition/TransitionGroup.java +++ b/core/java/android/view/transition/TransitionGroup.java @@ -17,13 +17,13 @@ package android.view.transition; import android.animation.Animator; import android.util.AndroidRuntimeException; +import android.util.ArrayMap; import android.util.LongSparseArray; import android.util.SparseArray; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; -import java.util.HashMap; /** * A TransitionGroup is a parent of child transitions (including other @@ -183,10 +183,10 @@ public class TransitionGroup extends Transition { */ @Override protected void prePlay(ViewGroup sceneRoot, - HashMap<View, TransitionValues> startValues, + ArrayMap<View, TransitionValues> startValues, SparseArray<TransitionValues> startIdValues, LongSparseArray<TransitionValues> startItemIdValues, - HashMap<View, TransitionValues> endValues, + ArrayMap<View, TransitionValues> endValues, SparseArray<TransitionValues> endIdValues, LongSparseArray<TransitionValues> endItemIdValues) { for (Transition childTransition : mTransitions) { @@ -200,10 +200,10 @@ public class TransitionGroup extends Transition { */ @Override protected void play(ViewGroup sceneRoot, - final HashMap<View, TransitionValues> startValues, + final ArrayMap<View, TransitionValues> startValues, final SparseArray<TransitionValues> startIdValues, final LongSparseArray<TransitionValues> startItemIdValues, - final HashMap<View, TransitionValues> endValues, + final ArrayMap<View, TransitionValues> endValues, final SparseArray<TransitionValues> endIdValues, final LongSparseArray<TransitionValues> endItemIdValues) { setupStartEndListeners(); diff --git a/core/java/android/view/transition/TransitionInflater.java b/core/java/android/view/transition/TransitionInflater.java index a5f5836..dc930d5 100644 --- a/core/java/android/view/transition/TransitionInflater.java +++ b/core/java/android/view/transition/TransitionInflater.java @@ -19,6 +19,7 @@ import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; +import android.util.ArrayMap; import android.util.AttributeSet; import android.util.SparseArray; import android.util.Xml; @@ -30,7 +31,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; /** * This class inflates scenes and transitions from resource files. @@ -40,8 +40,8 @@ public class TransitionInflater { // We only need one inflater for any given context. Also, this allows us to associate // ids with unique instances per-Context, used to avoid re-inflating // already-inflated resources into new/different instances - private static final HashMap<Context, TransitionInflater> sInflaterMap = - new HashMap<Context, TransitionInflater>(); + private static final ArrayMap<Context, TransitionInflater> sInflaterMap = + new ArrayMap<Context, TransitionInflater>(); private Context mContext; // TODO: do we need id maps for transitions and transitionMgrs as well? diff --git a/core/java/android/view/transition/TransitionManager.java b/core/java/android/view/transition/TransitionManager.java index 17f0d08..4971a92 100644 --- a/core/java/android/view/transition/TransitionManager.java +++ b/core/java/android/view/transition/TransitionManager.java @@ -19,8 +19,6 @@ import android.util.ArrayMap; import android.view.ViewGroup; import android.view.ViewTreeObserver; -import java.util.HashMap; - /** * This class manages the set of transitions that fire when there is a * change of {@link Scene}. To use the manager, add scenes along with @@ -38,9 +36,9 @@ public class TransitionManager { private static final Transition sDefaultTransition = new AutoTransition(); private Transition mDefaultTransition = new AutoTransition(); - HashMap<Scene, Transition> mSceneTransitions = new HashMap<Scene, Transition>(); - HashMap<Scene, HashMap<Scene, Transition>> mScenePairTransitions = - new HashMap<Scene, HashMap<Scene, Transition>>(); + ArrayMap<Scene, Transition> mSceneTransitions = new ArrayMap<Scene, Transition>(); + ArrayMap<Scene, ArrayMap<Scene, Transition>> mScenePairTransitions = + new ArrayMap<Scene, ArrayMap<Scene, Transition>>(); static ArrayMap<ViewGroup, Transition> sRunningTransitions = new ArrayMap<ViewGroup, Transition>(); @@ -92,9 +90,9 @@ public class TransitionManager { * using {@link AutoTransition}. */ public void setTransition(Scene fromScene, Scene toScene, Transition transition) { - HashMap<Scene, Transition> sceneTransitionMap = mScenePairTransitions.get(toScene); + ArrayMap<Scene, Transition> sceneTransitionMap = mScenePairTransitions.get(toScene); if (sceneTransitionMap == null) { - sceneTransitionMap = new HashMap<Scene, Transition>(); + sceneTransitionMap = new ArrayMap<Scene, Transition>(); mScenePairTransitions.put(toScene, sceneTransitionMap); } sceneTransitionMap.put(fromScene, transition); @@ -117,7 +115,7 @@ public class TransitionManager { // TODO: cached in Scene instead? long-term, cache in View itself Scene currScene = sceneRoot.getCurrentScene(); if (currScene != null) { - HashMap<Scene, Transition> sceneTransitionMap = mScenePairTransitions.get(scene); + ArrayMap<Scene, Transition> sceneTransitionMap = mScenePairTransitions.get(scene); if (sceneTransitionMap != null) { transition = sceneTransitionMap.get(currScene); if (transition != null) { diff --git a/core/java/android/view/transition/TransitionValues.java b/core/java/android/view/transition/TransitionValues.java index 120ace8..1ef6bf4 100644 --- a/core/java/android/view/transition/TransitionValues.java +++ b/core/java/android/view/transition/TransitionValues.java @@ -15,15 +15,16 @@ */ package android.view.transition; +import android.util.ArrayMap; import android.view.View; import android.view.ViewGroup; -import java.util.HashMap; +import java.util.Map; /** * Data structure which holds cached values for the transition. * The view field is the target which all of the values pertain to. - * The values field is a hashmap which holds information for fields + * The values field is a map which holds information for fields * according to names selected by the transitions. These names should * be unique to avoid clobbering values stored by other transitions, * such as the convention project:transition_name:property_name. For @@ -48,7 +49,7 @@ public class TransitionValues { /** * The set of values tracked by transitions for this scene */ - public final HashMap<String, Object> values = new HashMap<String, Object>(); + public final Map<String, Object> values = new ArrayMap<String, Object>(); @Override public String toString() { |