如果在首选项中设置,则告诉 Glide 不要加载图像
Tell Glide not to load images if set in preferences
我的应用程序中有几个 RecyclerView
,它们都有带有 ImageView
的项目,这些项目进一步填充了 Glide
,如下所示:
Glide.with(context)
.load(imageUrl)
.asBitmap()
.error(R.drawable.placeholder_avatar)
.centerCrop()
.into(mAvatarImageView);
在我的首选项屏幕中,用户可以禁用所有远程图像的加载以节省带宽。
如果不在所有 RecyclerView
适配器中使用经典的 if-else 条件,告诉 Glide
不要加载图像的最佳方法是什么,这违反了 DRY 原则?
我正在寻找这样的方法:
.shouldLoad(UserSettings.getInstance().isImageLoadingEnabled());
假设您使用的是 Glide v4,有一个专门为此目的设计的请求选项:RequestOptions.onlyRetrieveFromCache(boolean flag)
。启用后,仅加载内存或磁盘缓存中已存在的资源,有效防止网络加载并节省带宽。
如果您使用 Glide v4 Generated API,此选项可直接在 GlideApp.with(context).asBitmap()
返回的 GlideRequest
上使用。
否则,您必须创建启用此标志的 RequestOptions
和 apply
:
RequestOptions options = new RequestOptions().onlyRetrieveFromCache(true);
Glide.with(context).asBitmap()
.apply(options)
.error(R.drawable.placeholder_avatar)
.centerCrop()
.into(mAvatarImageView);
如果您决定使用 Kotlin
您可以创建所需的扩展函数:
fun <T> RequestBuilder<T>.shouldLoad(neededToLoad : Boolean) : RequestBuilder<T> {
if(!neededToLoad) {
return this.load("") // If not needed to load - remove image source
}
return this // Continue without changes
}
然后就可以使用了,如你问题所述:
Glide.with(context)
.load(imageUrl)
.shouldLoad(false)
.into(imageView)
公平地说,你只能创建一个 Kotlin
文件并使用 shouldLoad()
函数并在 Java
中使用它,但代码变得丑陋:
shouldLoad(Glide.with(this)
.load(imageUrl), false)
.into(imageView);
或
RequestBuilder<Drawable> requestBuilder = Glide.with(this)
.load(imageUrl);
requestBuilder = shouldLoad(requestBuilder, true);
requestBuilder.into(imageView);
我的应用程序中有几个 RecyclerView
,它们都有带有 ImageView
的项目,这些项目进一步填充了 Glide
,如下所示:
Glide.with(context)
.load(imageUrl)
.asBitmap()
.error(R.drawable.placeholder_avatar)
.centerCrop()
.into(mAvatarImageView);
在我的首选项屏幕中,用户可以禁用所有远程图像的加载以节省带宽。
如果不在所有 RecyclerView
适配器中使用经典的 if-else 条件,告诉 Glide
不要加载图像的最佳方法是什么,这违反了 DRY 原则?
我正在寻找这样的方法:
.shouldLoad(UserSettings.getInstance().isImageLoadingEnabled());
假设您使用的是 Glide v4,有一个专门为此目的设计的请求选项:RequestOptions.onlyRetrieveFromCache(boolean flag)
。启用后,仅加载内存或磁盘缓存中已存在的资源,有效防止网络加载并节省带宽。
如果您使用 Glide v4 Generated API,此选项可直接在 GlideApp.with(context).asBitmap()
返回的 GlideRequest
上使用。
否则,您必须创建启用此标志的 RequestOptions
和 apply
:
RequestOptions options = new RequestOptions().onlyRetrieveFromCache(true);
Glide.with(context).asBitmap()
.apply(options)
.error(R.drawable.placeholder_avatar)
.centerCrop()
.into(mAvatarImageView);
如果您决定使用 Kotlin
您可以创建所需的扩展函数:
fun <T> RequestBuilder<T>.shouldLoad(neededToLoad : Boolean) : RequestBuilder<T> {
if(!neededToLoad) {
return this.load("") // If not needed to load - remove image source
}
return this // Continue without changes
}
然后就可以使用了,如你问题所述:
Glide.with(context)
.load(imageUrl)
.shouldLoad(false)
.into(imageView)
公平地说,你只能创建一个 Kotlin
文件并使用 shouldLoad()
函数并在 Java
中使用它,但代码变得丑陋:
shouldLoad(Glide.with(this)
.load(imageUrl), false)
.into(imageView);
或
RequestBuilder<Drawable> requestBuilder = Glide.with(this)
.load(imageUrl);
requestBuilder = shouldLoad(requestBuilder, true);
requestBuilder.into(imageView);