1
votes

I have a listview of the images of different sizes. Images are loaded using glide library.

Currently I have set the height and width of the imageview as wrap_content to get the original feel of the image, but it's not behaving properly.

Therefore I want to set imageview width and height using aspect ratio.

I have used different scaleType, but not getting desired result.

Any help is appreciated

1
Generally you'd set one size (width or height) to be hard coded or constrained, the other to wrap_content, then set an appropriate scale type. If both are wrap_content its hard to know what to scale to.Gabe Sechan
@GabeSechan sure , I can keep width as fix. But Just curious to know, Can't i fix height and width using aspect ratio of the image? And what scale type i should use if i fix widthAwadesh
Lets say you have a 2:1 aspect ratio (twice as wide as high). If you fix the width and the height to 400, then either you only get a 200px tall image, (to match the width), or you have an 800 px wide image that doesn't all display (because its too big). Aspect ratio scaling doesn't make sense if both sizes are fixed. Unless you know the aspect ratio of any image that will be shown ahead of time.Gabe Sechan
I agree , thanks for the prompt reply. could you please help me, I want to create an UI for images like whatsapp what approach i should follow. I want to handle all sizes images as whatsapp handlesAwadesh

1 Answers

-1
votes

I would suggest you use the DimensionConstraints from ConstraintLayout. You can define an aspect ratio using the app:layout_constraintDimensionRatio attribute, so your ImageView can be something like this:

<ImageView android:layout_width="0dp"
                   android:layout_height="0dp"
                   app:layout_constraintDimensionRatio="H,2:1"
                   app:layout_constraintBottom_toBottomOf="parent"
                   app:layout_constraintTop_toTopOf="parent"/>

So the image view height will always be half the value of the width.