且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

drawable.setCallback(空)的后果;

更新时间:2023-02-05 22:59:02

您不应缓存可绘制 - 在绘制对象对象非常有状态的,并打算用在一个且只有一个所有者

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.

恒定状态检索与此:

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

(注意:此方法可以返回null;不是所有的可绘有恒定的状态)

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