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()
?
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! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With