繁体   English   中英

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

[英]Image loading slow while using Glide4 in RecyclerView to load images from url

我正在使用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 会自动清除缓存。 读这个 :

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

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

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

为了使加载速度更快尝试使用高速缓存由设置DiskCacheStrategy DiskCacheStrategy.NONEDiskCacheStrategy.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);
Glide.with( context )
            .load( arrayList.get().getImage_url())
            .thumbnail( 0.5f )
            .override( 200, 200 )
            .placeholder(R.drawable.placeholder_image)
            .diskCacheStrategy( DiskCacheStrategy.ALL )
            .into( imageView );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM