覆盖屏幕一半的宽度

问题描述 投票:0回答:1

我正在聊天,我使用Recyclerview来显示消息。当消息是图像时,我使用Glide进行显示。使用滑行,我使用覆盖功能显示具有特定尺寸的图像。但是,这并不是很好,因为并非所有手机都具有相同的分辨率和尺寸。

bitmap?.let {

           //meaning the image is landscape view
                    if (bitmap.width > bitmap.height)
                        Glide.with(Application.instance)
                            .load(fileInfo.localUri)
                            .apply(RequestOptions().override(500, 250))
                            .into(holder.sntImageView)
                    else
                        Glide.with(Application.instance)
                            .load(fileInfo.localUri)
                            .apply(RequestOptions().override(250, 500))
                            .into(holder.sntImageView)


                    holder.sendingProgress.visibility = View.GONE
                    holder.sntBubble.visibility = View.GONE
                    holder.sntImageView.visibility = View.VISIBLE

                } ?: run {
                    holder.sntImageView.visibility = View.GONE
                    holder.sendingProgress.visibility = View.GONE
                    holder.sntBody.text = "Unable to decode image"
                }

所以我的问题是如何使用Glide,以使图像几乎占据屏幕的一半,而不是500、250 ...?

android-recyclerview imageview android-glide
1个回答
0
投票

您可以从了解容器的容器开始,例如recyclerviews宽度:

container.width

这将为您提供以像素为单位的宽度。之后,您可以应用除法将像素减半:

val yourViewWidth = container.width / 2

这就是将其放入容器一半的方式。然后您只需要将该值传递给Glide。

如果要获得设备的总宽度,则可以获取像accepted answer这样的像素,然后应用相同的划分标准。

希望有帮助,编码愉快!

© www.soinside.com 2019 - 2024. All rights reserved.