gpt4 book ai didi

java - 在 RecyclerView 中使用 Glide4 从 url 加载图像时图像加载缓慢

转载 作者:行者123 更新时间:2023-12-02 09:13:29 39 4
gpt4 key购买 nike

我正在使用 Glide (4.6.1) 将图像从 url 加载到 RecyclerView ,与 Amazon 或 Flipkart 等应用程序相比,我的每个图像都非常慢大小约为170 KB。我不想使用缓存,因为我不会得到动态响应。有没有办法让图片加载更快。

public static void setGlide(Context context, ImageView imageView, String imageLink) {

RequestOptions requestOptions = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.centerCrop()
.dontAnimate()
.dontTransform()
.placeholder(R.drawable.error_logo)
.priority(Priority.IMMEDIATE)
.encodeFormat(Bitmap.CompressFormat.PNG)
.format(DecodeFormat.DEFAULT);

Glide.with(context)
.applyDefaultRequestOptions(requestOptions)
.load(imageLink")
.error(Glide.with(context)
.load(R.drawable.error_logo))
.into(imageView);
Glide.get(context).clearMemory();
}

build.gradle依赖项是

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha2'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
}

最佳答案

问题出在这一行:

 Glide.get(context).clearMemory();

你不需要这样做,glide会自动清除缓存。阅读此内容:

Although it’s good practice to clear loads you no longer need, you’re not required to do so. In fact, Glide will automatically clear the load and recycle any resources used by the load when the Activity or Fragment you pass in to Glide.with() is destroyed.

您在加载时清除了缓存,这不允许 glide 从缓存加载图像,而 glide 每次都会从互联网加载图像。如果要删除缓存,请在 Activity/Fragment/View 销毁时调用此函数。

尝试删除多余的行,因为 glide 会为您完成该任务,然后检查是否可以解决您的问题。

要加快加载速度,请尝试通过以下代码将 DiskCacheStrategy 从 DiskCacheStrategy.NONE 设置为 DiskCacheStrategy.AUTOMATIC 来使用缓存。

RequestOptions requestOptions = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.centerCrop()
.dontAnimate()
.dontTransform()
.placeholder(R.drawable.error_logo)
.priority(Priority.IMMEDIATE)
.encodeFormat(Bitmap.CompressFormat.PNG)
.format(DecodeFormat.DEFAULT);

关于java - 在 RecyclerView 中使用 Glide4 从 url 加载图像时图像加载缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207657/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com