Replace HashMap with SparseArray

This commit is contained in:
Andrew Rabert 2018-04-24 20:54:27 -04:00
parent b75db5bb86
commit bc0157c1b1

View File

@ -22,16 +22,15 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes; import android.support.annotation.AttrRes;
import android.support.annotation.DrawableRes; import android.support.annotation.DrawableRes;
import android.util.SparseArray;
import android.util.TypedValue; import android.util.TypedValue;
import net.nullsum.audinaut.R; import net.nullsum.audinaut.R;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
public class DrawableTint { public class DrawableTint {
private static final Map<Integer, Integer> attrMap = new HashMap<>(); private static final SparseArray<Integer> attrMap = new SparseArray<>();
private static final WeakHashMap<Integer, Drawable> tintedDrawables = new WeakHashMap<>(); private static final WeakHashMap<Integer, Drawable> tintedDrawables = new WeakHashMap<>();
public static Drawable getTintedDrawableFromColor(Context context) { public static Drawable getTintedDrawableFromColor(Context context) {
@ -47,24 +46,20 @@ public class DrawableTint {
} }
public static int getColorRes(Context context, @AttrRes int colorAttr) { public static int getColorRes(Context context, @AttrRes int colorAttr) {
int color; Integer color = attrMap.get(colorAttr);
if (attrMap.containsKey(colorAttr)) { if (color == null) {
color = attrMap.get(colorAttr);
} else {
TypedValue typedValue = new TypedValue(); TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme(); Resources.Theme theme = context.getTheme();
theme.resolveAttribute(colorAttr, typedValue, true); theme.resolveAttribute(colorAttr, typedValue, true);
color = typedValue.data; color = typedValue.data;
attrMap.put(colorAttr, color); attrMap.put(colorAttr, color);
} }
return color; return color;
} }
public static int getDrawableRes(Context context, @AttrRes int drawableAttr) { public static int getDrawableRes(Context context, @AttrRes int drawableAttr) {
if (attrMap.containsKey(drawableAttr)) { Integer attr = attrMap.get(drawableAttr);
return attrMap.get(drawableAttr); if (attr == null) {
} else {
int[] attrs = new int[]{drawableAttr}; int[] attrs = new int[]{drawableAttr};
TypedArray typedArray = context.obtainStyledAttributes(attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs);
@DrawableRes int drawableRes = typedArray.getResourceId(0, 0); @DrawableRes int drawableRes = typedArray.getResourceId(0, 0);
@ -72,6 +67,7 @@ public class DrawableTint {
attrMap.put(drawableAttr, drawableRes); attrMap.put(drawableAttr, drawableRes);
return drawableRes; return drawableRes;
} }
return attr;
} }
public static void wipeTintCache() { public static void wipeTintCache() {