Coil ImageView doesn't fit exactly
Asked Answered
T

4

9

According to Coils docs, I don't have to make any configuration for my image to fit(). The problem is, that the ImageView is not loading correctly:

This is my configuration for the ImageView with Picasso:

picasso.load(unsplashResult?.photoUrls?.thumbnailUrl)
                        .fit()
                        .error(R.drawable.img_placeholder)
                        .placeholder(R.drawable.img_placeholder)
                        .into(currentImageView)

And this is the code with Coil:

currentImageView.load(unsplashResult?.photoUrls?.thumbnailUrl) {
                        placeholder(R.drawable.img_placeholder)
                        error(R.drawable.img_placeholder)
                    }

The result on my ImageView is not the same though.

This is how Coil loads them:

enter image description here

And this is how Picasso loads them:

enter image description here

Question is, how can I achieve the same result with Coil?

EDIT

This is my ImageView:

<ImageView
            android:adjustViewBounds="true"
            android:id="@+id/ivUnsplashImage"
            android:src="@drawable/some_unsplash_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
Treaty answered 19/8, 2019 at 8:52 Comment(3)
hmmm. actually it says that there is no need to make any fit configurations. coil-kt.github.io/coil/migratingTreaty
on Coils README in githubTreaty
Were you been able to resolve this issue?Yehudit
V
7

Based on this document

You can work around this by setting the scaleType to centerCrop or scale(Scale.FILL) on your request.

https://coil-kt.github.io/coil/api/coil-base/coil.request/-request-builder/scale/

https://coil-kt.github.io/coil/api/coil-base/coil.size/-scale/

you are set Scale type this way

view.load(chatMessageBean.thumb) {
       crossfade(750)
       placeholder(errorPlaceHolder)
       transformations(CircleCropTransformation())
       error(errorPlaceHolder)
       scale(Scale.FILL)
}
Villada answered 5/9, 2019 at 7:3 Comment(3)
just one thing, this behavior does resolve, but on second load it still persistsTreaty
@coroutineDispatcher Can you elaborate more in details?Villada
well this piece of code does work, but if I click the same fragment again, the photos will get back to the previous state (the not correct one, picture 1 in the question)Treaty
J
2

So, according to migration guide answer should be like;

imageView.scaleType = ImageView.ScaleType.FIT_CENTER

or it should be defined in XML

android:scaleType="fitCenter"
Junkojunkyard answered 19/8, 2019 at 9:54 Comment(4)
Nope, same resultTreaty
did you check this? github.com/coil-kt/coil/issues/20 i think there is an issue with scalingClementia
@coroutineDispatcher Looks like this is related to this issue (github.com/coil-kt/coil/issues/20). I'm working on fixing it for the 0.7.0 release in a couple days.Shiprigged
yes, but in my case it still doesn't give the expected result, even after setting the scaletype to centercropTreaty
C
2

As much I understood from the documentation you should use ImageView.ScaleType

imageView.scaleType = ImageView.ScaleType.FIT_XY

or you can use it into your xml tag of your ImageView

android:scaleType="fitXY"

Apart from FIT_XY you can use ImageView.ScaleType.CENTER_CROP too according to your needs.

Covin answered 2/11, 2020 at 8:15 Comment(0)
H
0

Just remove the line android:adjustViewBounds="true" in your layout XML file.

Hensel answered 27/3, 2020 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.