diff options
| author | 2016-07-19 15:41:27 +0000 | |
|---|---|---|
| committer | 2016-07-19 15:41:28 +0000 | |
| commit | c606e64ebc5d3eb5fc76bad55e76ae1190a23f6d (patch) | |
| tree | 7826d89d0ef4e0d9d3d349d980550814f5dbd51f | |
| parent | ae65e9fb7ffbf5f769d4bdc1bf224c18d23beb2f (diff) | |
| parent | 64f3352493447ff8a5e4faa2fd34714ff5334eca (diff) | |
Merge "Add drawable caching"
| -rw-r--r-- | tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java b/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java index ea320c701c24..c3d4cef61b35 100644 --- a/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java +++ b/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java @@ -45,6 +45,7 @@ import android.content.res.Resources.Theme; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.DisplayMetrics; +import android.util.LruCache; import android.util.TypedValue; import android.view.ViewGroup.LayoutParams; @@ -58,6 +59,9 @@ import java.util.Iterator; public class Resources_Delegate { private static boolean[] mPlatformResourceFlag = new boolean[1]; + // TODO: This cache is cleared every time a render session is disposed. Look into making this + // more long lived. + private static LruCache<String, Drawable.ConstantState> sDrawableCache = new LruCache<>(50); public static Resources initSystem(BridgeContext context, AssetManager assets, @@ -75,6 +79,7 @@ public class Resources_Delegate { * would prevent us from unloading the library. */ public static void disposeSystem() { + sDrawableCache.evictAll(); Resources.mSystem.mContext = null; Resources.mSystem.mLayoutlibCallback = null; Resources.mSystem = null; @@ -137,9 +142,23 @@ public class Resources_Delegate { @LayoutlibDelegate static Drawable getDrawable(Resources resources, int id, Theme theme) { Pair<String, ResourceValue> value = getResourceValue(resources, id, mPlatformResourceFlag); - if (value != null) { - return ResourceHelper.getDrawable(value.getSecond(), resources.mContext, theme); + String key = value.getSecond().getValue(); + + Drawable.ConstantState constantState = key != null ? sDrawableCache.get(key) : null; + Drawable drawable; + if (constantState != null) { + drawable = constantState.newDrawable(resources, theme); + } else { + drawable = + ResourceHelper.getDrawable(value.getSecond(), resources.mContext, theme); + + if (key != null) { + sDrawableCache.put(key, drawable.getConstantState()); + } + } + + return drawable; } // id was not found or not resolved. Throw a NotFoundException. |