landscapist icon indicating copy to clipboard operation
landscapist copied to clipboard

Height issue with ContentScale.FillWidth

Open luiscurini opened this issue 2 years ago • 4 comments

  • Library Version: landscapist-coil:1.5.2
  • Affected Device: Emulator Android 10, Samsung devices Android 11/12

Describe the Bug:

I wanted to add rounded corners to images that I'm loading, their contentScale is set to ContentScale.FillWidth, so I'm expecting that once the image is loaded the bounds are updated and the clip() modifier is applied. I added a border so the issue can be visible.

HorizontalPager(
            count = content.size,
            state = pagerState,
            modifier = Modifier.fillMaxHeight(fraction = 0.67f),
            verticalAlignment = Alignment.CenterVertically,
            contentPadding = PaddingValues(horizontal = 23.dp)
        ) { page ->
            content.getOrNull(page)?.let {
                CoilImage(
                    imageModel = it.location,
                    contentDescription = it.fileName,
                    contentScale = ContentScale.FillWidth,
                    // FIXME: 24.05.22 found a solution for rounded corners.
                    modifier = Modifier
                        .padding(horizontal = 7.dp)
                        .fillMaxWidth()
                        .clip(
                            RoundedCornerShape(16.dp)
                        )
                        .border(0.5.dp, color = grayscale20, shape = RoundedCornerShape(16.dp))
                )
        }
}
Screenshot 2022-05-25 at 10 28 29

Expected Behavior:

This actually works if I use Coil 2.0 directly

HorizontalPager(
            count = content.size,
            state = pagerState,
            modifier = Modifier.fillMaxHeight(fraction = 0.67f),
            verticalAlignment = Alignment.CenterVertically,
            contentPadding = PaddingValues(horizontal = 23.dp)
        ) { page ->
            content.getOrNull(page)?.let {
                AsyncImage(
                    model = it.location,
                    contentDescription = it.fileName,
                    contentScale = ContentScale.FillWidth,
                    modifier = Modifier
                        .padding(horizontal = 7.dp)
                        .fillMaxWidth()
                        .clip(
                            RoundedCornerShape(16.dp)
                        )
                        .border(0.5.dp, color = grayscale20, shape = RoundedCornerShape(16.dp))
                )
            }
        }
Screenshot 2022-05-25 at 10 33 59

luiscurini avatar May 25 '22 08:05 luiscurini

@luiscurini Sorry for the late response. What about you giving a specific height size for the CoilImage?

skydoves avatar Jun 15 '22 12:06 skydoves

Hi problem @skydoves, well the idea with this pager is to load different images from the content URIs. They can occupy the maxHeight of the parent as long as FillWidth is respected.

luiscurini avatar Jun 15 '22 13:06 luiscurini

Thanks for the details. Let me check out the issue soon!

skydoves avatar Jun 15 '22 13:06 skydoves

I'm running into the same problem. I don't get the problem in compose preview using previewHolder, but run into it on emulator runs. (setting the background as red here to illustrate)

image

devandanger avatar Aug 14 '22 22:08 devandanger

I am seeing the same issue with the GlideImage. I think the issue might be in GlideImage in line 169

val drawable = glideImageState.drawable ?: return@ImageRequest
          imageOptions.LandscapistImage(
            modifier = Modifier.fillMaxSize(), // issue here?
            painter = rememberDrawablePainter(
              drawable = drawable,
              imagePlugins = component.imagePlugins
            )
          )

The LandscapistImage doesn't use the passed modifier but I am not sure, just a guess

antonb03 avatar Sep 30 '22 17:09 antonb03

As a workaround, the success callback can be used

CoilImage(
     imageModel = it.location,
     success = { coilImageState ->
          val drawable = coilImageState.drawable
          Image(
               painter = rememberDrawablePainter(drawable = drawable),
               contentScale = ContentScale.FillWidth,
               contentDescription = it.filename
               modifier = Modifier
                    .padding(horizontal = 7.dp)
                    .fillMaxWidth()
                    .clip(
                         RoundedCornerShape(16.dp)
                    )
                   .border(0.5.dp, color = grayscale20, shape = RoundedCornerShape(16.dp))
          )
     }
)

antonb03 avatar Sep 30 '22 18:09 antonb03

Thanks for tinkering with this issue @antonb03! Let me check more details and get back to you soon.

skydoves avatar Oct 01 '22 01:10 skydoves

Hi guys, I built the codes below with version 2.0.0 and it seems to work for me.

Screen Shot 2022-10-03 at 10 50 25 AM

Box(modifier = Modifier.padding(12.dp)) {
    GlideImage(
      modifier = Modifier.fillMaxWidth()
        .clip(RoundedCornerShape(16.dp))
        .border(0.5.dp, color = Color.Gray, shape = RoundedCornerShape(16.dp)),
      imageOptions = ImageOptions(contentScale = ContentScale.FillWidth),
      imageModel = { R.drawable.virtual_background },
      component = rememberImageComponent {
        +CircularRevealPlugin()
        +PalettePlugin { palette = it }
      },
      previewPlaceholder = R.drawable.poster
    )
  }

Please try to the new release and if you still face the same issue, I'd appreciate it if you could share some examples. Thanks!

skydoves avatar Oct 03 '22 01:10 skydoves

I created a sample project that reproduces the problem. You can check it here https://github.com/antonb03/fillwidthexample

antonb03 avatar Oct 12 '22 23:10 antonb03

@antonb03 Could you give me the expected result as well? In this case, you should specify the height size like the below:

  GlideImage(
    modifier = Modifier
      .height(350.dp)
      .clip(RoundedCornerShape(16.dp))
      .border(0.5.dp, color = Color.Gray, shape = RoundedCornerShape(16.dp))
      .background(color = Color.Red),
    imageOptions = ImageOptions(contentScale = ContentScale.FillWidth),
    imageModel = {"https://picsum.photos/id/237/200/300"}
  )

skydoves avatar Oct 15 '22 06:10 skydoves

In my case, I don't want to hardcode the image height because the images from the url can be of various heights. Also, screen width is different for different devices and it is nicer UI when the height of the image is scaled proportionally to the width instead of being hardcoded. The expected behavior is to fill max width of the screen and the height should be scaled proportionally.

I can add some examples later to the sample project I build to show the expected behavior

antonb03 avatar Oct 18 '22 14:10 antonb03

Hi everyone, this issue has been fixed since version 2.1.0. Thank you for the reports!

skydoves avatar Nov 12 '22 05:11 skydoves

Just tested it and it works as expected. Thank you so much! @luiscurini I think the issue can be closed

antonb03 avatar Nov 14 '22 16:11 antonb03

Awesome. Thank you for checking these, @antonb03!

skydoves avatar Nov 14 '22 22:11 skydoves