0

I am developing a chat and i use Recyclerview to show the messages. When the message is an image i use Glide to display it. Using glide i use the override function to show the image with specific dimensions. However this does not work very good because not all the phones have the same resolution and size.

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"
                }

So my question is how to use Glide so that the image has almost the half of the screen instead of 500, 250...?

james04
  • 1,580
  • 2
  • 20
  • 46

1 Answers1

0

You can start by knowing the with of your container, like recyclerviews width:

container.width

This will give you the width in pixels. After that you can apply a division to get the pixels at half:

val yourViewWidth = container.width / 2

That's how you make it to half of the container. And then you just have to pass that value to Glide.

If you want to get total device width, you can get the pixels like this accepted answer and then apply the same division criteria.

Note that if you are in a recyclerview and want to know the size of a child view, you may need to wait the layout to be ready in order to have access to its parameters. That can be done with the ViewTreeObserver.OnGlobalLayoutListener, And do your operations inside onGlobalLayout. It will be triggered when the view is ready and you'll be able to operate there.

Hope it helps, happy coding!

Joan
  • 321
  • 1
  • 10