开发者

Consequences of drawable.setCallback(null);

开发者 https://www.devze.com 2023-04-11 04:25 出处:网络
While trying to implement small in-memory cache of Drawables, I learned that to avoid memory leaks after closing activity I need to unbind those Drawables: set their callback to null.

While trying to implement small in-memory cache of Drawables, I learned that to avoid memory leaks after closing activity I need to unbind those Drawables: set their callback to null.

Because maintaining Drawables cached in each activity would require extra code, I tried to unbind them immediately after setImageDrawable(drawable) and I don't see any consequences so far.

This is code from MyImageView class (ext开发者_如何学Cends ImageView):

setImageDrawable(drawable);
d.setCallback(null);

In debugger I can clearly see that before first line callback is null, after first line it is set to this imageView, and after that I set it to null again. It is normally shown after that..

Documentation for setCallback (Drawable.Callback cb) states:

Bind a Drawable.Callback object to this Drawable. Required for clients that want to support animated drawables.

Since I don't need animated drawable, I don't see why I shouldn't do this but it bothers me that in several blogs about memory leakage in Android concerning drawables this is done only after activity is done. Question is, why is callback always automatically set when binding to ImageView?

Are there some border conditions where those drawables with callback set to null will cause a problem? Not displaying or NPE?


You should not cache Drawables -- the Drawable object is very stateful, and intended to be used by one and only one owner.

If you want to implement a cache, you should be caching the drawable's constant state.

The constant state is retrieve with this:

http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getConstantState()

(Note this method can return null; not all Drawables have constant state.)

You can later instantiate new Drawables from a constant state with this:

http://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState.html#newDrawable(android.content.res.Resources)

Also keep in mind that Resources already maintains a cache of Drawables for you, using this facility, so there is no need for you to implement your own cache for any Drawables you are retrieving from Resources.

And if you are making your own Drawables outside of resources, I would strongly recommend making a cache of the underlying data (such as a bitmap downloaded from the network) then trying to mess with the constant state. (And again, definitely don't cache Drawable objects themselves.)

0

精彩评论

暂无评论...
验证码 换一张
取 消