Townsfolk
Townsfolk

Reputation: 1324

ImageView fill width of parent, clip any extra height

I have an ImageView that's displaying a rectangular image. I want to display a review of an image captured from a custom camera activity.

The issue is that the captured image is larger (in height) than the view and so is scaling by X to fit the Y.

enter image description here

As seen in the image, there's a white stripe along the right side of the image. What I want is for the image to fill the width of the view and just clip or hide the extra at the bottom.

I've tried a bunch of different configurations but can't seem to get it to work. Currently I have:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:clipChildren="true"
     android:clipToPadding="true">
     <ImageView
          android:id="@+id/camera_review"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:scaleType="fitStart" />
     ....
</FrameLayout>

The image is going to be cropped into the square view-port so it's important that the image be displayed from top|left, and fill the width.

One way I was able to get it to work was to put the ImageView into a ScrollView, but then I have to hide the scrollbars, and figure out how to disable scrolling.

Upvotes: 3

Views: 1726

Answers (3)

Akash Moradiya
Akash Moradiya

Reputation: 3322

Chnage your code like this. It may help you.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:clipChildren="true"
         android:clipToPadding="true">
         <ImageView
              android:id="@+id/camera_review"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
               android:adjustViewBounds="true"
                android:scaleType="fitXY" />
         ....
    </FrameLayout>

Upvotes: 0

Jibran Khan
Jibran Khan

Reputation: 3256

According to Moradiya Akash answer, the image will fit your ImageView but no accuracy of aspect ratio. Steve Haley had an answer on maintaining the aspect ratio.

Make sure you're setting the image to the ImageView using android:src="..." rather than android:background="...". src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView . More here

Upvotes: 1

balaji koduri
balaji koduri

Reputation: 1321

change the tag in your image view "fitStart" to "fitXY"

android:scaleType="fitXY"

Upvotes: 0

Related Questions