Dave
Dave

Reputation: 29121

ImageView with wrap_content height shows very skinny / cropped photo instead of full height

I have a screen where the user can read an article (with large photo at the top.

It's general structure is:

LinearLayout -> ScrollView -> LinearLayout -> ImageViews,TextViews...etc

The ImageView I'm having problems with is the large image at the top (below the Article's title, but above it's text:

        <ImageView
            android:id="@+id/article_photo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/super_light_gray"
            android:scaleType="centerCrop"
            android:contentDescription="@string/articlePhoto"
            android:cropToPadding="true"
            android:layout_marginBottom="10dp"
            android:padding="1dp"/>

The problem is, the photos appear, but are cropped vertically A LOT so that even vertical-oriented photos show up very skinny and wide. I assumed that "wrap_content" should make it the full height of the photo... but I'm obviously wrong :)

How can I make the photo show up full width (already working), but also full height?

I'm using the UniversalImageLoader to load the images:

        if(lrgPhoto != null && !lrgPhoto.filepath.equals(""))
        {
            imageLoader.displayImage(
                "http://img.mysite.com/processes/resize_android.php?image=" + lrgPhoto.filepath + "&size=640&quality=90",
                imageView,
                imgDisplayOptions
                );
            imageView.setVisibility(View.VISIBLE); //show photo
        }
        else
        {
            imageView.setVisibility(View.GONE); //hide photo
            imageView.invalidate();
        }

Edit:

If I change 'scaleType' to 'fitXY', then the image shows correct dimensions without being cropped, but then it's not full-width. Is there a way I can have both full-width AND not-cropped strangely?

It appears the "crop" is because it's using the original height of the image for the height, but it's enlarging the image to fit full-width...

Upvotes: 4

Views: 4594

Answers (3)

George Mincila
George Mincila

Reputation: 529

Add this to the imageview xml:

android:adjustViewBounds="true"

Upvotes: 30

raydowe
raydowe

Reputation: 1315

I would have thought this was straightforward, but it looks like it might not be possible with xml layout alone:

ImageView - have height match width?

Upvotes: 0

baconcheese113
baconcheese113

Reputation: 963

Change

android:scaleType="centerCrop"

to

android:scaleType="fitXY"

Upvotes: 1

Related Questions