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

Alf*_*fin 1 java groovy android

我使用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();
}
Run Code Online (Sandbox Code Playgroud)

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'
}
Run Code Online (Sandbox Code Playgroud)

Jay*_*gar 6

问题出在这一行:

\n\n
 Glide.get(context).clearMemory();\n
Run Code Online (Sandbox Code Playgroud)\n\n

你不需要这样做,glide 会自动清除缓存。读这个 :

\n\n
\n

尽管清除不再需要的负载是一种很好的做法,但您并不需要这样做。事实上,当你传入 Glide.with() 的 Activity 或 Fragment 被销毁时,Glide 会自动清除负载并回收负载使用的任何资源。

\n
\n\n

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

\n\n

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

\n\n

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

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