Resize image to full width and variable height with Picasso

Resize image to full width and variable height with Picasso

center inside requires calling resize with positive width and height picasso
picasso android
picasso fit cannot be used with resize
how to resize image in imageview
picasso height in feet
picasso crop image
android resize image to specific size programmatically
get image width and height from url android

I have a listView with an adapter that contains ImageView of variable size (width and height). I need resize the pictures load with Picasso to the max width of layout and a variable height given by the aspect ratio of the picture.

I have checked this question: Resize image to full width and fixed height with Picasso

The fit() works but I haven't found nothing to keep the aspect ratio of the picture.

This code partially works if I fixed the height in the layout of the adapter:

Picasso.with(this.context).load(message_pic_url)
.placeholder(R.drawable.profile_wall_picture)
.fit().centerInside()
.into(holder.message_picture);

But it generates blank spaces between the pictures of the listView because the pictures may be that not have that height.

Thanks in advance.


As of Picasso 2.4.0, this operation is now directly supported. Simply add a .resize() request with one of the dimensions as 0. For example, to have a variable width, your call would become:

Picasso.with(this.context)
       .load(message_pic_url)
       .placeholder(R.drawable.profile_wall_picture)
       .resize(0, holder.message_picture.getHeight()),
       .into(holder.message_picture);

Note that this call uses .getHeight() and therefore assumes the message_picture has already been measured. If that isn't the case, such as when you have inflated a new view in a ListAdapter, you can delay this call until after measurement by adding an OnGlobalLayoutListener to the view:

holder.message_picture.getViewTreeObserver()
      .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            // Wait until layout to call Picasso
            @Override
            public void onGlobalLayout() {
                // Ensure we call this only once
                imageView.getViewTreeObserver()
                         .removeOnGlobalLayoutListener(this);


                Picasso.with(this.context)
                       .load(message_pic_url)
                       .placeholder(R.drawable.profile_wall_picture)
                       .resize(0, holder.message_picture.getHeight())
                       .into(holder.message_picture);
            }
        });

Resize image to full width and variable height with Picasso, I need resize the pictures load with Picasso to the max width of layout and a variable height given by the aspect ratio of the picture. I have checked this question:  There is a way for you to resize your photos in Picasa. With the help of the Export Tool, you will be able to resize your photo. Also, keep in mind that when you resize your photo, the original file of that image will not be altered. To do this, see step 1.


I came across the same issue and it took me a while to track down a solution but I finally came across something that works for me.

Firstly I changed the Picasso call to

Picasso.with(this.context).load(message_pic_url)
.placeholder(R.drawable.profile_wall_picture)
.into(holder.message_picture);

Removing the fit and the centerInside. Next you need to add the following lines to the ImageView in your XML

android:scaleType="fitStart"
android:adjustViewBounds="true"

Hopefully it will work for you as well.

Resize image to full width and variable height with Picasso , I have a listView with an adapter that contains ImageView of variable size (width and height). I need resize the pictures load with Picasso to the  Picasso.with (context).load (UsageExampleListViewAdapter.eatFoodyImages).resize (600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio.into (imageViewResize); Use of scaleDown () When using the resize () option Picasso will also upscale your image.


Finally I solved it doing a transformation of Picasso, here is the snippet:

    Transformation transformation = new Transformation() {

        @Override
        public Bitmap transform(Bitmap source) {
            int targetWidth = holder.message_picture.getWidth();

            double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            int targetHeight = (int) (targetWidth * aspectRatio);
            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
            if (result != source) {
                // Same bitmap is returned if sizes are the same
                source.recycle();
            }
            return result;
        }

        @Override
        public String key() {
            return "transformation" + " desiredWidth";
        }
    };

    mMessage_pic_url = message_pic_url;

    Picasso.with(this.context)
        .load(message_pic_url)
        .error(android.R.drawable.stat_notify_error)
        .transform(transformation)
        .into(holder.message_picture, new Callback() {
            @Override
            public void onSuccess() {
                holder.progressBar_picture.setVisibility(View.GONE);
            }

            @Override
            public void onError() {
                Log.e(LOGTAG, "error");
                holder.progressBar_picture.setVisibility(View.GONE);
            }
    });

This line is for customize with your desired width:

int targetWidth = holder.message_picture.getWidth();

Additionally this snipped include Callback for loading hide and error drawable built-in Picasso.

If you need more information to debug any error, you MUST implement a custom listener (Picasso builder) beacuse the onError Callback information is "null". You only know that there is an error for UI behavior.

I hope this helps someone to save many hours.

Resize with keep aspect ratio · Issue #226 · square/picasso · GitHub, I'd like to be able to resize an image by keeping a ratio aspect. One example would be for a resize on width: Picasso.with(cont the display area and I need increasing it display area based on the width (could be height). For example, in your 8MP image world Picasso decodes the full image before  i'm using picasso library to download and load images into imageView. now i want to know how i can get image width and height before loading them in imageViews ?. i have a listview with an adapter that contains two imageView(one of them is vertical and another is horizontal). depends on image width and height i want to load image into one of the imageviews.


May Accepted Answer is Useful to all but If you are binding Multiple ViewHolder for Multiple Views then you can reduce your code by Creating Class for Transformation and passing ImageView from ViewHolder.

/**
 * Created by Pratik Butani
 */
public class ImageTransformation {

    public static Transformation getTransformation(final ImageView imageView) {
        return new Transformation() {

            @Override
            public Bitmap transform(Bitmap source) {
                int targetWidth = imageView.getWidth();

                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                int targetHeight = (int) (targetWidth * aspectRatio);
                Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                if (result != source) {
                    // Same bitmap is returned if sizes are the same
                    source.recycle();
                }
                return result;
            }

            @Override
            public String key() {
                return "transformation" + " desiredWidth";
            }
        };
    }
}

Calling from ViewHolder:

Picasso.with(context).load(baseUrlForImage)
                     .transform(ImageTransformation.getTransformation(holder.ImageView1))
                     .error(R.drawable.ic_place_holder_circle)
                     .placeholder(R.drawable.ic_place_holder_circle)
                     .into(holder.mMainPhotoImageView1);

Hope it will Help you.

Resize image to full width and variable height with Picasso, I have a listView with an adapter that contains ImageView of variable size (width and height). I need resize the pictures load with Picasso to the max width of  Picasso Does Not Preserve Aspect Ratio While Resizing Image. to full width and variable height with Picasso. 4. Resize image to full width and fixed height


    Picasso.with(this).load(url).resize(1800, 1800).centerInside().into(secondImageView)

    <ImageView
        android:id="@+id/SecondImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:adjustViewBounds="true"
        android:layout_margin="10dp"
        android:visibility="gone"/>

This will help you with variable height of images for all devices

Picasso, Picasso — Image Resizing, Scaling and fit() In the last blog posts, you've learned how to load images from Picasso Series Overview The image will be displayed completely, but might not fill the entire ImageView . I'm just confirming that this worked for me. I have the imageView xml set to centerInside then i just specify a width, in my case it is the width of the screen, and then it maintains the aspect ratio. I just pass in 0 for the height. Picasso.with(context).load(url).resize(width, 0).into(imageView); 👍


Resize image to full width and variable height with Picasso, center inside requires calling resize with positive width and height picasso picasso android picasso fit cannot be used with resize android picasso compress​  Scale a bitmap to fit height or width. the ImageView has been measured and resize the image to exactly match its size. of the imageView because we didn’t call resize. Picasso.with(this


Displaying Images with the Picasso Library, resize(width, height) during the Picasso load and explicitly set a width or height for the image such as: Picasso.with().load(imageUri).resize(500, 0).into() . By​  Free photo resizer and image compressor to crop, resize images in JPEG|PNG|GIF format to the exact pixels or proportion you specified, compress them to reduce the file sizes, making it easy to use them as your desktop wallpaper, Facebook cover photo, Twitter profile photo, avatar icons, etc.


Scale a bitmap to fit height or width - JPARDOGO, Picasso is one of these libraries, in my opinion the best, that's why I use it in my applications and I will In some cases we don't know the final height/width we want to display. Resize the image to the specified size in pixels. use android:scaleType="centerInside" ImageView.ScaleType CENTER_INSIDE Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).