ImageView with one of its dimensions set to wrap_content does not work
See original GitHub issueDescribe the bug Assume the following ImageView:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
If you try to load an image to it, Coil will not calculate the correct width (based on the ImageView’s fixed height and the aspect ratio of the image being loaded), and the ImageView will end up with a 0 width.
Using size()
solves this issue, but it is not ideal because it is not always possible to know beforehand the dimensions of the image being loaded.
To Reproduce
Load an image to an ImageView which has one fixed dimension, and one wrap_content
dimension.
Sample project: CoilWrapContent.zip
Expected behavior
Coil should automatically calculate the dimension set to wrap_content
based on the fixed dimension & the dimensions of the image being loaded.
For example: If I load a 400x200 (2:1 aspect ratio) image to an ImageView with android:layout_width="wrap_content"
and android:layout_height="100dp"
, then Coil should automatically set the width to 200dp (perhaps this shouldn’t apply to all scaleTypes).
Logs/Screenshots Layout Inspector screenshot showing the ImageView having its width set to 0:
Library version 0.6.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:9 (5 by maintainers)
Top GitHub Comments
@fernandospr You can work around this by setting the
scaleType
tocenterCrop
orscale(Scale.FILL)
on your request.This occurs because
wrap_content
is evaluated to 1px whereas Glide and Picasso treat it as the height of the parent. I’m working on fixing this behaviour for0.7.0
.Good catch! I think this is because we force
PixelSize
’s dimensions to be > 0. Relaxing that limitation to >= 0 should solve the issue, but might cause side-effects. Going to work on this later tonight.