问题描述
我正在使用glide在我的android应用程序中加载图像,以避免在我使用应用程序上下文加载图像时发生任何崩溃.这将对应用程序和内存的性能产生什么影响?
I am using glide for image loading in my android app, to avoid any crashes I am loading images with application context. What will be effect of this on performance of application and memory?
推荐答案
Glide提供许多.with()
方法的原因是:它遵循生命周期.
Glide provides so many .with()
methods for a reason: it follows lifecycle.
设想一个动态添加到活动的Fragment
.在其onCreateView
方法中,它将启动3MB图像的Glide加载.现在,如果用户按下后退"按钮并删除了片段"或关闭了整个活动,该怎么办?
Imagine a Fragment
that is dynamically added to an Activity. In its onCreateView
method it starts a Glide load of a 3MB image. Now, what if the user presses the back button and the Fragment is removed or the whole activity is closed?
- 如果使用
with(getActivity().getApplicationContext())
则什么也不会发生,将下载所有3MB的数据,然后对其进行解码,缓存,甚至可能设置为ImageView,然后对其进行垃圾回收,因为对它的唯一引用是来自Glide内部./li> - 如果使用
with((Fragment)this)
,Glide会订阅Fragment的生命周期事件,并且一旦Fragment停止,任何未完成的请求都应暂停;销毁后,所有待处理的请求都会被清除.这意味着图像下载将中途停止,并且该死片段将不再使用任何资源. - 如果您使用
with(getActivity())
,则Glide订阅了Activity的生命周期事件,并且与上述情况相同,但仅当Activity停止或销毁时才发生.
- If you use
with(getActivity().getApplicationContext())
nothing will happen, all 3MBs of data is downloaded and then decoded, cached, probably even set to the ImageView, which is then garbage collected, because the only reference to it was from Glide internals. - If you use
with((Fragment)this)
Glide subscribes to the Fragment's lifecycle events and as soon as the Fragment is stopped, the any outstanding request should be paused; and when destroyed, all pending requests be cleared. This means that the image download will stop midway and no more resources will be used by that dead Fragment. - If you use
with(getActivity())
Glide subscribes to the Activity's lifecycle events and the same thing happens as above, but only when the Activity is stopped or destroyed.
因此,最佳实践是使用最接近的上下文/片段,以避免未完成的请求完成! (还有一种手动方式来停止加载:Glide.clear(ImageView|Target)
.)
So the best practice is to use the closest possible context/fragment to avoid unused request completions! (There's also a manual way to stop a load: Glide.clear(ImageView|Target)
.)
要在实践中应用此方法,请尝试在可能的情况下使用with(this)
,但在不适用时(例如在适配器中或集中式图像加载方法中),请传入RequestManager glide
作为参数,并使用glide.load(...
例如:
To apply this in practice try to use with(this)
when possible, but when it's not, like in an adapter, or a centralized image loading method, pass in a RequestManager glide
as an argument and use glide.load(...
, for example:
static loadImage(RequestManager glide, String url, ImageView view) {
glide.load(url).into(view);
}
或在适配器中
class MyAdapter extends WhichEveryOneYouUse {
private final RequestManager glide;
MyAdapter(RequestManager glide, ...) {
this.glide = glide;
...
}
void getView/onBindViewHolder(... int position) {
// ... holder magic, and get current item for position
glide.load... or even loadImage(glide, item.url, holder.image);
}
}
并从活动/片段"中使用它们:
and use these from Activity/Fragment:
loadImage(Glide.with(this), url, findViewById(R.id.image));
// or
list.setAdapter(new MyAdapter(Glide.with(this), data));
这篇关于使用应用程序上下文滑动图像加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!