drawable.setCallback(空)的后果; [英] Consequences of drawable.setCallback(null);

查看:602
本文介绍了drawable.setCallback(空)的后果;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图实现小内存高速缓存可绘中,我了解到,为了避免关闭的活动我需要解除那些可绘制后内存泄漏:设置自己的回调为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.

由于保持可绘制缓存中的每个活动都需要额外的code,我试图解除他们的立即 setImageDrawable(绘制)和我没有看到任何后果至今。
这是MyImageView类(扩展的ImageView )code:

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 (extends ImageView):

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

在调试器中我可以清楚地看到,之前第一行的回调为空,第一行后,它被设置为这个ImageView的,之后,我将其设置为再次空。这之后的正常显示。

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

文档 setCallback(Drawable.Callback CB)规定:

绑定一个Drawable.Callback对象,这可绘制。必需适合希望支持动画可绘制的客户。

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

因为我不需要动画绘制,我不明白为什么我不应该这样做,但它困扰我,在一些网友关于内存泄漏的Andr​​oid有关可绘制这样做活动完成之后。问题是,为什么是回调总是在绑定到的ImageView

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?

有没有在这里的可绘有回调设置为null会导致问题的一些边界条件?不显示或NPE?

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.

恒定状态检索与此:

<一个href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getConstantState%28%29">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:

<一个href="http://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState.html#newDrawable%28android.content.res.Resources%29">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.)

这篇关于drawable.setCallback(空)的后果;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆