[英]Image loading slow while using Glide4 in RecyclerView to load images from url
[英]Glide loads images from firebase painfully slow using URLs
我正在尝试创建一个由每个单元格中的 ImageViews 填充的 RecyclerView,每个图像对应于 Firebase 存储中的一个图像。 我有一个传递到我的 RecyclerView 适配器的字符串列表,每个字符串都代表 URL 到 Firebase 存储中的图像。 我在onBindViewHolder()
中加载每个图像。
我得到的回报是加载几张图片(大约 5 张图片)的速度非常慢,然后加载另外 5 张图片大约需要 4 分钟,而且在这些图片之后似乎永远不会加载任何其他图片。
我已经阅读了 StackOverflow 上的多篇文章,但其中大多数只是告诉你使用fitCenter()
或centerCrop()
,但这并没有改变我的情况。 我还在 Glide 的文档中读到 Glide 会自动对您的图像进行下采样,所以我不需要手动进行,对吗? 有什么想法我可能在这里做错了吗? Url 字符串已从 Firebase 成功检索,查询几乎立即得到解决,所以我认为那里没有任何问题。
更新:我对onBindViewHolder()
方法进行了一些修改,以便明确请求 Glide 中的图像缓存,并且我还使用thumbnail API
来下载较低分辨率的图像。 现在正在加载更多图像,但每张仍然需要大约 7 秒才能加载,这显然太长了。 如果您有任何建议,请告诉我。
以下是在我的主要活动中设置 RecyclerView 的方式:
iconsRCV = findViewById(R.id.cardIconsRCV)
iconsRCV.layoutManager = GridLayoutManager(this,5) // set the layout manager for the rcv
val iconUrls : ArrayList<String> = ArrayList() // initialize the data with an empty array list
val adapter = CardIconAdapter(this,iconUrls) // initialize the adapter for the recyclerview
iconsRCV.adapter = adapter // set the adapter
请注意,完成某些查询后我会获得新数据,然后我调用adapter.notifyDataSetChanged()
将新数据传递给 RecyclerView。
CardIconAdapter.java:
public class CardIconAdapter extends RecyclerView.Adapter<CardIconAdapter.ViewHolder> {
private RequestOptions requestOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL).centerCrop().error(R.drawable.applogotmp);
private List<String> urlsList;
private Context context;
class ViewHolder extends RecyclerView.ViewHolder {
ImageView iconImg;
ViewHolder(@NonNull View view) {
super(view);
iconImg = view.findViewById(R.id.cardIcon);
}
}
public CardIconAdapter(Context cntxt, List<String> data) {
context = cntxt;
urlsList = data;
}
@NonNull
@Override
public CardIconAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_icons_rcv_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CardIconAdapter.ViewHolder holder, int position) {
GlideApp.with(context).load(urlsList.get(position)).apply(requestOptions).into(holder.iconImg);
}
@Override
public int getItemCount() {
return urlsList.size();
}
}
PS Firebase 中的图像大小大多是 udner 200KB,但也有少数达到 4MB。 此外, R.layout.card_icons_rcv_item
布局中的 ImageView 大小为 75x75。
希望你使用了最新版本的 glide。 很少有更好的图像加载和缓存方法,感谢这篇好文章。
1.启用磁盘缓存
val requestOptions = RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)
Glide.with(context).load(url).apply(requestOptions).into(imageView)
2.列表项
val requestOptions = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.signature(ObjectKey(signature))
Glide.with(context).load(url).apply(requestOptions).into(imageView)
3.覆盖图像大小(可选)
val requestOptions = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.signature(ObjectKey(signature))
.override(100, 100) // resize does not respect aspect ratio
Glide.with(context).load(url).apply(requestOptions).into(imageView)
4.添加缩略图Url
// With thumbnail url
Glide.with(context).load(url)
.thumbnail(Glide.with(context).load(thumbUrl))
.apply(requestOptions).into(imageView)
// Without thumbnail url
// If you know thumbnail size
Glide.with(context).load(url)
.thumbnail(Glide.with(context).load(url).apply(RequestOptions().override(thumbSize)))
.apply(requestOptions).into(imageView)
// With size multiplier
Glide.with(context).load(url)
.thumbnail(0.25f)
.apply(requestOptions).into(imageView)
5. 设置每月清洁计划
// This method must be called on the main thread.
Glide.get(context).clearMemory()
Thread(Runnable {
// This method must be called on a background thread.
Glide.get(context).clearDiskCache()
}).start()
6. 改造bitmap
// TODO remove after transformation is done
.diskCacheStrategy(SOURCE) // override default RESULT cache and apply transform always
.skipMemoryCache(true) // do not reuse the transformed result while running
.diskCacheStrategy(DiskCacheStrategy.ALL) // It will cache your image after loaded for first time
.format(DecodeFormat.PREFER_ARGB_8888) //for better image quality
.dontTransform() // to load image faster just skip transform
.placeholder(R.drawable.placeholder) // use place holder while image is being load
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.