android - 毕加索在 android 中使用匹配父项获取的最佳实践
问题描述
我正在使用 Picasso 下载图片并在 RecyclerView 中显示它们。我试图防止在滚动 RecyclerView 和 Picasso 正在下载新图片时发生的小延迟,所以我正在使用 fetch。当我为图片使用精确尺寸时,它运行良好,但现在我在我的 xml 中使用“匹配父级”(在所有设备上工作),从那时起有一个延迟,当我向后滚动时第四,它不记得图片(延迟已经看过的图片)。我尝试了 fit() 和 centerCrop() 但它很慢,所以这是我目前解决问题的尝试:
在适配器中(这只运行一次)我设置了一个监听器来获取 ImageViews 的确切大小
private void preloadImage(final View picture) {
ViewTreeObserver observer = picture.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
PictureUtil.fetchImage(picture.getHeight(), picture.getWidth());
ViewTreeObserver mObserver = picture.getViewTreeObserver();
mObserver.removeOnGlobalLayoutListener(this);
}
});
}
在 PictureUtil 中,我获取所有图片:
public static void fetchImage(int height, int width) {
if (height > 0 && width > 0) {
for (House house : Houses.houseList) {
Picasso.get().load(getFrontPicturePath(house.id))
.resize(width, height)
.centerCrop()
.fetch();
}
}
}
在适配器中,这就是我加载它们的方式:
Picasso.get()
.load(PictureUtil.getFrontPicturePath(houseId))
.error(PictureUtil.getPlaceholderPath(context))
.fit()
.centerCrop()
.into(view);
这是xml中的imageview:
<ImageView
android:id="@+id/house_row_picture"
android:layout_width="match_parent"
android:layout_height="170dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/img_small" />
因此,我再次遇到图像延迟的问题(不像我有固定尺寸时),当我在 Recyclerview 中滚动时,图像消失并再次加载。我究竟做错了什么?对此有最佳做法吗?
提前致谢
解决方案
推荐阅读
- shell - 递归查找文件大小大于 1 GB 的目录中的文件
- azure - consumer.azure.com 的 v3 API 大部分时间都无法返回 offerId
- navigationbar - SwiftUI navigationBarTitle - displayMode: .inline - 崩溃
- excel - 使用 Powershell 从 Excel 工作表中获取复选框?
- reactjs - Material-UI 中添加的自托管字体不起作用?
- javascript - 如何使用 php 标签在 grid.js 中编写获取文本
- sql-server - 如何在 SQL 和 LINQ 中获取每个员工的第二个最高工资值?
- openxml - 我在 C# 中使用 OpenXML 创建 xlsx 文件,但日期为 1900 年 1 月
- c++ - 如何优雅地实现具有可变输出的单一方法?
- collection-select - 为什么我不能使用这个代码 f.collection_select