recyclerView 内的 ImageView 正确尺寸

发布于 2025-01-13 08:00:34 字数 2426 浏览 4 评论 0原文

我正在开发一个聊天 Android 应用程序。在 chatRecyclerView 中,我想显示图像以及所有其他视图。但是,由于我不知道每个图像的大小,因此我无法在 xml 中预先定义 img 的宽度-高度。如果我用wrap_content、wrap_content定义那些,这会使回收器滚动不平滑,并且图像显示得非常大或非常小。

我想出了如下的解决方案。

       <ImageView
            android:id="@+id/img_rcv_msg_image_landscape"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="32dp"
            android:adjustViewBounds="true"
            android:maxWidth="320dp"
            android:maxHeight="320dp"
            android:minWidth="150dp"
            android:minHeight="150dp"
            android:scaleType="centerCrop"
            android:src="@drawable/error_image"
            app:layout_constraintBottom_toBottomOf="@+id/img_frame"
            app:layout_constraintEnd_toEndOf="@+id/img_frame"
            app:layout_constraintStart_toStartOf="@+id/img_frame"
            app:layout_constraintTop_toBottomOf="@+id/img_rcvName"
            tools:ignore="ContentDescription" />

       <ImageView
            android:id="@+id/img_rcv_msg_image_portrait"
            android:layout_width="wrap_content"
            android:layout_height="280dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="32dp"
            android:adjustViewBounds="true"
            android:maxWidth="320dp"
            android:maxHeight="320dp"
            android:minWidth="150dp"
            android:minHeight="150dp"
            android:scaleType="centerCrop"
            android:src="@drawable/error_image"
            app:layout_constraintBottom_toBottomOf="@+id/img_frame"
            app:layout_constraintEnd_toEndOf="@+id/img_frame"
            app:layout_constraintStart_toStartOf="@+id/img_frame"
            app:layout_constraintTop_toBottomOf="@+id/img_rcvName"
            tools:ignore="ContentDescription" />

我有 2 个图像视图。一种用于横向模式,一种用于纵向模式。我在 ViewHolder 中拥有的唯一数据是图像文件,我不知道哪一个是要显示的正确图像。

val file = messageItem.messageItemToFile(itemView.context, it) ?: return@let

//Hide both
imageViewLand.visibility = View.GONE
imageViewPortrait.visibility = View.GONE

Glide
     .with(itemView.context)
     .load(file)
     .diskCacheStrategy(DiskCacheStrategy.NONE)
     .into(
        if (file_width > file_height) imageViewLand else imgeViewPortrait)
     )

有更好的解决方案吗?

I am developing a chat Android application. Inside the chatRecyclerView i want to show Images along with all other views. However, since i dont know the size of each image i cannot pre-define inside the xml the width-height of the img. If i define those with wrap_content, wrap_content, this makes the recycler scroll without smoothness and also the image to show very big or very small.

I came up with a solution like the below.

       <ImageView
            android:id="@+id/img_rcv_msg_image_landscape"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="32dp"
            android:adjustViewBounds="true"
            android:maxWidth="320dp"
            android:maxHeight="320dp"
            android:minWidth="150dp"
            android:minHeight="150dp"
            android:scaleType="centerCrop"
            android:src="@drawable/error_image"
            app:layout_constraintBottom_toBottomOf="@+id/img_frame"
            app:layout_constraintEnd_toEndOf="@+id/img_frame"
            app:layout_constraintStart_toStartOf="@+id/img_frame"
            app:layout_constraintTop_toBottomOf="@+id/img_rcvName"
            tools:ignore="ContentDescription" />

       <ImageView
            android:id="@+id/img_rcv_msg_image_portrait"
            android:layout_width="wrap_content"
            android:layout_height="280dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="32dp"
            android:adjustViewBounds="true"
            android:maxWidth="320dp"
            android:maxHeight="320dp"
            android:minWidth="150dp"
            android:minHeight="150dp"
            android:scaleType="centerCrop"
            android:src="@drawable/error_image"
            app:layout_constraintBottom_toBottomOf="@+id/img_frame"
            app:layout_constraintEnd_toEndOf="@+id/img_frame"
            app:layout_constraintStart_toStartOf="@+id/img_frame"
            app:layout_constraintTop_toBottomOf="@+id/img_rcvName"
            tools:ignore="ContentDescription" />

I have 2 imageViews. One for the LandScape and one for the Portrait mode. The only data that i have in the ViewHolder is the File of the Image and i dont know which one is the proper image to show.

val file = messageItem.messageItemToFile(itemView.context, it) ?: return@let

//Hide both
imageViewLand.visibility = View.GONE
imageViewPortrait.visibility = View.GONE

Glide
     .with(itemView.context)
     .load(file)
     .diskCacheStrategy(DiskCacheStrategy.NONE)
     .into(
        if (file_width > file_height) imageViewLand else imgeViewPortrait)
     )

Is there a better solution to come up with??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文