user1915767
user1915767

Reputation: 111

Change ImageView size in a recyclerview after Picasso.load()?

I have:

Current situation: The image is loaded into the imageView, but the imageview's size is the recycled grid item's imageview's size.

What I would like to achieve: After loading the image into the imageview, resize the imageview on runtime (redraw).

What I have tried:

  1. notifyItemChanged() - could do the trick (in theory at least), but I am not able to check if the current grid item's view is in layout state, so my app crashed with IllegalStateException
  2. listening to Picasso's load with a Callback, and onSuccess() check the imageview drawable aspectratio and try to resize the imageview itself with requestLayout(). Did not work. (Well it worked, but only when there is an animation or something triggering the redraw of the layout. If there is nothing, then the imageview is not redrawn.)
  3. listening to Picasso's load with a Callback, and onSuccess() start an animation animating the alpha of the imageview. This would trigger the redraw. But this sometimes worked sometimes not(and I don't know why).

Upvotes: 3

Views: 5187

Answers (3)

oguzhan
oguzhan

Reputation: 2179

I wanna show this using a

Campaign Banner List Using Dynamic Width - Height Example

that:

  • Create an item_campaign_banner.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">

    <RelativeLayout
            android:id="@+id/campaignImageSuperView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <ImageView
                android:id="@+id/campaignImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/campaign_image_error_icon"
                android:visibility="visible"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"/>

        <View
                android:id="@+id/campaignSectionLine"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:layout_below="@+id/campaignImage"
                android:background="#F2F2F2" />

    </RelativeLayout>


</RelativeLayout>
  • Than create a BannerHolder.java
public class BannerHolder extends RecyclerView.ViewHolder {

    private ImageView campaignImage;
    private View campaignSectionLine;
    private ViewGroup campaignImageSuperView;

    public BannerHolder(@NonNull View itemView) {
        super(itemView);
        campaignImage = (ImageView) itemView.findViewById(R.id.campaignImage);
        campaignSectionLine =  itemView.findViewById(R.id.campaignSectionLine);
        campaignImageSuperView =  itemView.findViewById(R.id.campaignImageSuperView);
    }

    public ImageView getCampaignImage() {
        return campaignImage;
    }

    public ViewGroup getCampaignImageSuperView() {
        return campaignImageSuperView;
    }
}
  • Than apply this in onBindViewHolder(@NonNull RecyclerView.ViewHolder incomingHolder, int position) method that your created YourAdapter.java
if (incomingHolder instanceof BannerHolder) {

    BannerHolder bannerHolder = (BannerHolder) incomingHolder;
            Banner banner = (Banner) campaigns.get(position);

            if(banner != null && banner.getImage() != null && banner.getImage().getWidth() != null && banner.getImage().getHeight() != null) {
                Picasso.with(activity)
                        .load(banner.getImage().getUrl())
                        .error(R.drawable.campaign_image_error_icon)
                        .into(bannerHolder.getCampaignImage());

                bannerHolder.getCampaignImageSuperView().setMinimumWidth(banner.getImage().getWidth());
                bannerHolder.getCampaignImageSuperView().setMinimumHeight(banner.getImage().getHeight());
            }
        }

That's all!

Upvotes: 0

Max Makeichik
Max Makeichik

Reputation: 227

What I did was putting an ImageView into FrameLayout and then changing this FrameLayout's size to needed. Hope it would help you.

<FrameLayout
        android:id="@+id/frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">

        <ImageView
            android:id="@+id/video_thumbnail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitStart"
            android:adjustViewBounds="true" />

</FrameLayout>

P.S. changing FrameLayout's size:

viewHolder.frame.setMinimumWidth(neededWidth);

Upvotes: 4

encastellano
encastellano

Reputation: 445

Picasso has a function to resize your image. Something like this:

Picasso.with(context)
  .load(url)
  .resize(50, 50)
  .centerCrop()
  .into(imageView)

You can change centerCrop, to play with aspect ratio

Upvotes: 0

Related Questions