Consequences of drawable.setCallback(null);

回眸只為那壹抹淺笑 提交于 2019-11-30 03:57:47

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.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!