我有一个 RecyclerView(它实际上是一个聊天)。用户可以发送图像和文本。问题是当我滚动时感觉滞后,因为正在加载图像(如果我上下移动,毕加索需要很长时间才能将图像重新带回来)。我正在使用这行代码来做到这一点。
Picasso.with(itemView.getContext())
.load(((ChatMediaMessage) message).getUrl())
.into(imageView);
如果我使用调整大小选项根本不滞后,但图像显然会失去图像比例。
Picasso.with(itemView.getContext())
.load(((ChatMediaMessage) message).getUrl())
.resize(400, 400)
.into(imageView);
ViewHolder 的布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp">
<com.stfalcon.chatkit.utils.ShapeImageView
android:id="@id/messageUserAvatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginRight="8dp" />
<com.google.android.flexbox.FlexboxLayout
android:id="@id/bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/message_incoming_bubble_margin_right"
android:layout_toRightOf="@id/messageUserAvatar"
android:orientation="vertical"
app:alignContent="stretch"
app:alignItems="stretch"
app:flexWrap="wrap"
app:justifyContent="flex_end">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside" />
<TextView
android:id="@id/messageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@id/messageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/messageText"
android:layout_marginLeft="8dp"
app:layout_alignSelf="center" />
</com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>
你试过滑翔吗?试一试。只需在依赖项上添加库,只替换毕加索字。
您的图像大小是否更大,例如 2Mb 以上?在将图像上传到任何地方之前尝试调整图像大小或优化图像。
你的图片是JPG吗?您可以使用 RIOT http://luci.criosweb.ro/riot/来优化您的 JPG/PNG。
或者建议将您的 JPG 图像转换为 WebP。Android Studio 有内置的转换器可以尝试。WebP 加载速度更快,体积更小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句