在RecyclerView中使用Glide4从URL加载图像时,图像加载速度很慢 [英] Image loading slow while using Glide4 in RecyclerView to load images from url

查看:185
本文介绍了在RecyclerView中使用Glide4从URL加载图像时,图像加载速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Glide (4.6.1)将图像从url加载到 RecyclerView ,与A​​mazon或flipkart之类的应用相比,它非常慢,我的每个图像大小约为 170 KB .我不想使用缓存,因为我不会得到动态响应.有什么方法可以使图像加载更快.

I'm using Glide (4.6.1) for loading image from url to RecyclerView and it is very slow compared to apps like Amazon or flipkart, my each image size is approx 170 KB. I don't want to use cache because I won't get dynamic response. Is there any way to make the image loading faster.

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();

您不需要这样做,滑动会自动清除缓存.阅读此内容:

You don't need to do it, glide automatically clears the cache. Read this :

尽管清除不需要的负载是一种很好的做法,但您可以不需要这样做.实际上,Glide会自动清除在活动"或您传递给Glide.with()的片段已被破坏.

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.

您正在加载时清除缓存,这不允许滑行从缓存中加载图像,这反过来每次都会从Internet加载图像.如果要删除缓存,请在活动/片段/视图破坏时调用此函数.

You are clearing the cache at the loading time, which does not allow glide to load image from cache,which in turn load the image everytime from the internet. If you want to remove caching, Call this function when activity/fragment/view is destroys.

尝试移除多余的线路,因为滑行会为您完成该任务,然后检查是否可以解决您的问题.

Try removing the that redundant line as glide does that task for you,after that check if that resolves your issue.

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

To make loading faster try using cache by setting DiskCacheStrategy from DiskCacheStrategy.NONE to DiskCacheStrategy.AUTOMATIC by following code.

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);

这篇关于在RecyclerView中使用Glide4从URL加载图像时,图像加载速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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