【发布时间】: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,图像加载速度更快,但图像质量很差...