我正在做一个聊天应用程序。为此,如果消息是图像类型,我想包装高度并固定宽度。但问题是,由于图像需要一些时间来加载,因此聊天消息会发生变化,直到图像被加载,因此存在混蛋。我该如何处理?
<android.support.constraint.ConstraintLayout
android:id="@+id/parent_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/send_message_box"
android:maxWidth="228dp">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/iv_message"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="200dp"
android:layout_height="0dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:padding="11dp"
android:visibility="gone" />
</android.support.constraint.ConstraintLayout>
这是我正在使用的图像布局。
您可以修复高度并显示占位符图像,直到图像加载为止。您可以使用Picasso获得显示占位符图像(直到图像加载)的功能,请检查此https://square.github.io/picasso/
您可以在下载图像时使用默认图像设置初始高度。下载图像后,如果将高度设置为wrap_content
,则将占用图像的高度。因此,您需要修改您的ImageView
,如下所示。
<ImageView
android:id="@+id/iv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@mipmap/ic_image_loader_placeholder" />
请根据您的要求修改ImageView
。
你也可以看看这个Github project,我已经实现了类似的想法。我不得不调用Flickr的API并将图像加载到RecyclerView
中。希望有所帮助!