3

我需要将一个数据对象放入包含软引用的弱​​哈希图中。如何将我的“可绘制”对象转换为软引用?

WeakHashMap <String, SoftReference<Drawable>> tempPulled = new WeakHashMap<String,     SoftReference<Drawable>>();
Drawable pulled = BitmapDrawable.createFromResourceStream(null, null, conn.getInputStream(), "galleryImage", options);
SoftReference<Drawable> sPulled;            
tempPulled.put(id, pulled);

tempPulled 应该是 "sPulled" ,即软引用

4

2 回答 2

3

I think this is what you are looking for

SoftReference<Drawable> sPulled = new SoftReference<Drawable>(pulled);            

But you probably want to add a queue to remove the SoftRefence from the map should the referenced object be removed (or your cache will grow with keys and empty softrefences.

于 2011-12-10T21:40:44.567 回答
0

Take a look at Guava's MapMaker class, which allows you to specify soft values. Way easier than reimplementing it yourself.

于 2011-12-10T21:38:43.553 回答