【问题标题】:Image loading slow while using Glide4 in RecyclerView to load images from url在 RecyclerView 中使用 Glide4 从 url 加载图像时图像加载缓慢
【发布时间】:2020-03-31 02:57:39
【问题描述】:

我正在使用 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'
}

【问题讨论】:

  • 从 url 加载图像需要一些时间,您可以使用占位符在加载时显示图像。
  • 你能添加你的适配器类吗?
  • 170kb 对于在移动设备上显示的图像来说是很多 TBH。它最大应该在 50kb 左右,对于平板电脑它可以达到 100k。如果它(应用程序)与图像质量无关,请尝试降低它。或者也许给用户一个选择图像质量的选项。但这会在后端产生开销,因为您必须保留所有图像。
  • 尝试在 RequestOptions 中添加 .override(width, height) 并选择您对图像质量和加载速度感到满意的宽度和高度。
  • 我已将图像大小从 1 Kb 减小到 5 Kb,图像加载速度更快,但图像质量很差...

标签: java android groovy


【解决方案1】:

问题在于这一行:

 Glide.get(context).clearMemory();

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

虽然清除不再需要的负载是一种很好的做法,但 不需要这样做。实际上,Glide 会自动清除 加载并回收加载时使用的任何资源 Activity 或 你传递给 Glide.with() 的片段被销毁。

您在加载时清除了缓存,这不允许 glide 从缓存中加载图像,而这反过来又每次都从 Internet 加载图像。如果要删除缓存,请在销毁活动/片段/视图时调用此函数。

尝试删除多余的行,因为 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);

【讨论】:

  • 我已经按照您指定的方式修改了代码,但只有一点点改进...感谢您的反馈
  • 第一次加载图片需要很长时间。之后图像加载速度更快,是否被缓存.. 是否有任何替代代码可以更快地加载图像而不被缓存?
  • 因为你使用的是缓存,所以第一次请求加载需要很长时间,之后它只会从缓存中获取图像,这将减少加载时间。您可以使用占位符来查看加载进度。
  • 是否有任何替代代码可以更快地加载图像而不被缓存...
  • 您可以清除缓存并将 DiskCacheStrategy 更改为 NONE ,这将删除缓存并在每次您要加载图像时调用请求。我建议你不要这样做,因为性能会降低。使用缓存并没有什么害处。如果你想让 UI 变得更好并且图像加载不那么无聊,你可以使用占位符。
【解决方案2】:
Glide.with( context )
            .load( arrayList.get().getImage_url())
            .thumbnail( 0.5f )
            .override( 200, 200 )
            .placeholder(R.drawable.placeholder_image)
            .diskCacheStrategy( DiskCacheStrategy.ALL )
            .into( imageView );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    相关资源
    最近更新 更多