Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coil ImageView doesn't fit exactly

Tags:

android

coil

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"/>
like image 701
coroutineDispatcher Avatar asked Aug 19 '19 08:08

coroutineDispatcher


People also ask

What is adjust view bounds in android?

android:adjustViewBounds—If set to true, the attribute adjusts the bounds of the ImageView control to maintain the aspect ratio of the image displayed through it. android:resizeMode—The resizeMode attribute is used to make a control resizable so we can resize it horizontally, vertically, or around both axes.


Video Answer


2 Answers

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)
}
like image 186
Mayur Patel Avatar answered Oct 23 '22 11:10

Mayur Patel


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"
like image 21
Ferhat Ergün Avatar answered Oct 23 '22 09:10

Ferhat Ergün