Search code examples
androidimagepicasso

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 ?


Solution

  • 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);