Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso .fit().centerCrop() not working with WRAP_CONTENT

I have an ImageView that I create programmatically, and load an image into using Picasso. The height and width of the ImageView are set to WRAP_CONTENT. The image is pretty large, so I want to use Picasso's .fit().centerCrop() methods in order to save memory. However, when I add the methods, the images don't show up, and I think it must be because of WRAP_CONTENT. Here's my code:

ImageView iv = new ImageView(getContext());
RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(centerParams);
Picasso.with(context)load(path).fit().centerCrop().into(iv); //if I remove .fit().centerCrop(), the images load just fine

The only info I could find on this was this issue on GitHub about it -- while the issue says it was closed in v2.4, some people have commented saying that they are experiencing it in v2.5. (I'm using v2.5 as well)

EDIT: Here's what I'm using now, per Angela's suggestion:

ImageView iv = new ImageView(getContext());
RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(350));
centerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
iv.setLayoutParams(centerParams);
iv.setAdjustViewBounds(true);                                    
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getContext()).load(path).fit().into(iv);

The images now show up, but they aren't scaling properly, the aspect ratio gets changed somehow. Any ideas on how to preserve aspect ratio while still allowing Picasso to get a measurement for .fit()?

like image 405
weirdo16 Avatar asked Sep 22 '16 00:09

weirdo16


1 Answers

I also encountered that problem. Here's what I did...

            Picasso
                    .with(context)
                    .load(path)
                    .fit()
                    .into(iv);

In the xml file try to include scaleType, adjustViewBounds, and layout_gravity for example:

<ImageView
                android:id="@+id/img_movie"
                android:layout_width="match_parent"
                android:layout_height="350dp"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                />

Hope this helps! :)

like image 146
Angela Sanchez Avatar answered Oct 06 '22 00:10

Angela Sanchez