Doug Ray
Doug Ray

Reputation: 1010

Resizing image with picasso

Hey everyone I have been running into an issue with Picasso. I am trying to load an image from sound cloud but it keeps appearing stretched or very small. Here is my XML

        <ImageView
            android:id="@+id/album_cover_art"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/fragment_content_item_top_margin"
            android:scaleType="centerInside"
            android:contentDescription="@string/content_description_image_placeholder"
            android:src="@drawable/placeholder" />

I tried to use picasso's resize and center inside but the image appears small.

Picasso.with(getContext()).load(mSelectedTrack.getArtworkURL()).resize(800,300).centerInside().into(mAlbumCoverArt);

Using picasso's resize and center crop maintains the image view size but leads to the image appearing stretched.

Picasso.with(getContext()).load(mSelectedTrack.getArtworkURL()).resize(800,300).centerCrop().into(mAlbumCoverArt);

Any ideas on a way to do this that is easier than writing a function to resize it myself ?

Upvotes: 2

Views: 12623

Answers (3)

Abhi
Abhi

Reputation: 13

Try setting height or width to fixed and other to wrap content and load Picasso with .fitXY and don't use . resize.

Upvotes: 0

Flavio Gasparini
Flavio Gasparini

Reputation: 71

I avoided image stretching using :

.resize(800,0)

Passing 0 as second parameter it preserved the image proportions.

Hope it helps

Upvotes: 7

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this:

for your imageview:

<ImageView
android:id="@id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"  //add this
android:scaleType="fitCenter" />

And in picasso use .fit().centerCrop()

Picasso.with(getContext()).load(mSelectedTrack.getArtworkURL()).resize(800,300).fit().centerCrop().into(mAlbumCoverArt);

Upvotes: 4

Related Questions