1

I have an ImageView inside of a LinearLayout which is an item of a RecyclerView. I have all these views' width as MATCH_PARENT.

Now, I'm trying to calculate the heigth using the aspect ratio before the image is loaded so there's no resizing.

But, what I've seen is that before the image is loaded, its width is zero.

The thing is that I'm doing similar things in other parts of the app and it's working fine, I don't really know what I'm missing here.

RecyclerView item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">

    <LinearLayout

        ...

    </LinearLayout>

    <!-- This is the image -->
    <ImageView
        android:id="@+id/descubre_shop_banner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorLightText"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginBottom="8dp"/>

</LinearLayout>

ViewHolder

mBannerTarget = new Target()
{
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
    {
        mShopBannerImageView.setImageBitmap(bitmap);

        // Wrap content when the images is loaded.
        mShopBannerImageView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mShopBannerImageView.setBackgroundColor(-1);
        mShopBannerImageView.setAlpha(1.0f);

        Animation fadeOut = new AlphaAnimation(0, 1);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setDuration(250);
        mShopBannerImageView.startAnimation(fadeOut);

        LOADED = true;
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable)
    {
        mShopBannerImageView.setBackgroundColor(
                mContext.getResources().getColor(android.R.color.holo_red_dark));

        mShopBannerImageView.setAlpha(0.2f);
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable)
    {
        // This is 0
        Log.d(Properties.TAG, "" + mShopBannerImageView.getWidth());

        // Remove the old bitmap
        mShopBannerImageView.setImageBitmap(null);

        // Set the height using the width and the aspect ratio.
        mShopBannerImageView.getLayoutParams().height =
                (int) (mShopBannerImageView.getWidth() * shop.getAspectRatio());

        // Set a color while loading.
        mShopBannerImageView.setBackgroundColor(
                mContext.getResources().getColor(R.color.colorAccent));
        mShopBannerImageView.setAlpha(0.25f);
    }
};

I tried to set the width of the ImageView and its parent as MATCH_PARENT in code but no luck. Any clue of what's going on?

Regards,

Dani M
  • 1,173
  • 1
  • 15
  • 43

1 Answers1

0

The views dimensions is always 0 before being fully created, if you wanted to know the actual width, you have to use the method: post():

view.post(new Runnable() {
            @Override
            public void run() {
                //   This is where width would be different than 0
            }
        });

That being said, you should actually use an other method to keep the ratio than actually calculating it yourself! See this thread for further information How to scale an Image in ImageView to keep the aspect ratio