I am trying to store drawables in a static SoftReferenceMap for a ListView containing progress-bars in every row. Every row has a different progress, but when I set it through setProgress(int) all rows get affected.
I tried doing mDrawable.mutate().setLevel(progress*10000/max), but it still doesn't work.
public void setProgres(int status, int progress, int max) {
Drawable d = getDrawable(status);
switch (status) {
case RED_INDETERMINATE:
setIndeterminateDrawable(d);
setIndeterminate(true);
break;
case YELLOW:
d.mutate().setLevel((int) (progress * 10000 / max));
case GREEN:
case BLUE:
setProgressDrawable(d);
break;
default:
throw new IllegalArgumentException(
"Invalid Status setting for ArtooProgressBar: " + status);
}
getProgressDrawable().invalidateSelf();
super.invalidate();
}
private Drawable getDrawable(int status) {
SoftReference<Drawable> ref = sDrawableMaps.get("" + status);
if (ref != null && ref.get() != null)
return ref.get();
Drawable d;
switch (status) {
case RED_INDETERMINATE:
d = getResources().getDrawable(
R.drawable.progress_bar_indeterminate);
d = tileifyIndeterminate(d);
break;
case YELLOW:
// d = yellow;
d = getResources().getDrawable(
R.drawable.progress_bar_determinate_yellow);
break;
case GREEN:
d = getResources().getDrawable(
R.drawable.progress_bar_determinate_green);
d.setLevel(10000);
break;
case BLUE:
d = getResources().getDrawable(
R.drawable.progress_bar_determinate_blue);
d.setLevel(10000);
bre开发者_如何学编程ak;
default:
throw new IllegalArgumentException(
"Invalid Status setting for ArtooProgressBar: " + status);
}
if (d != null)
sDrawableMaps.put("" + status, new SoftReference<Drawable>(d));
d.invalidateSelf();
return d;
}
What am I missing?
Thanks
It looks like you are sharing the same Drawable instance across multiple views. Basically, don't do that. Why are you doing this sDrawableMaps cache thing instead of just calling Resources.getDrawable() for each Drawable you need? It is likely the cause of trouble.
Also your use of mutate() is not going to do what you think -- if the Drawable is not already mutable, then this returns a new Drawable instance which you then modify and nobody actually sees. If it is already mutable, it does return the same instance, but in that case there was no reason to call mutate() on it.
精彩评论