I'm trying to display a photo with so that it fills the display vertically, and is scrollable horizontally. My photos are of arbitrary dimensions, but always in landscape aspect ratio.
I placed an ImageView inside a HorizonalScrollView, and it almost works, except the aspect ratio is off. The ImageView is resized to fit exactly vertically, but the image is horizontally stretched or compressed.
I've tried every combination of values for scaleType and adjustViewBounds, and nothing works.
Can it be done with pure XML or do I have to resize my views in Java?
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/image_photo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/panoramic_1" />
</HorizontalScrollView>
Thanks in advance...
The problem is the:
android:scaleType="fitXY"
It scales in X and Y independently, so that imageview matches the maximum visible width and height.
Change to:
android:scaleType="centerInside"
This will keep the original aspect.
More info here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Related
I am trying to resize an image by keeping the aspect ratio. It should be just large enough to fill the screen width and if necessary some of the image should be off-screen.
here is a picture of what I want
I tried
android:adjustViewBounds
but when the image is larger than screen it fits image into imageView and not filling the width.
And I also don't want to use :
android:scaleType="centerCrop"
Here is my complete code:
<RelativeLayout 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">
<ImageView
android:id="#+id/imageBackgroundAboutFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/about_us_background"/>
</RelativeLayout>
Use android:scaleType="fitXY" with android:adjustViewBounds
UPDATE
I think you should do some trial and errors :
How to scale an Image in ImageView to keep the aspect ratio
Scale Image to fill ImageView width and keep aspect ratio
So many possible fix, but i dont really sure which one will work for you (my answer working for me but not for you - for example).
updtae your imageview like this..
remove scaleType and adjustViewBounds.. and make layout_height as match_parent
<ImageView
android:id="#+id/imageBackgroundAboutFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/about_us_background"/>
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.
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.
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
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>
change the tag in your image view "fitStart" to "fitXY"
android:scaleType="fitXY"
I am trying to set an Image that I am downloading from the web. I am using a library to help me do this called ion. The problem is that I need to resize the image to fit my screen and keep the aspect ration. The height seems to be working fine however I want to set the width of the bitmap to fit the whole width of the devicewhile keeping the aspect ratio. But it is not working and the image is cut short on both sides...
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="#dimen/_height" />
Code:
DisplayMetrics metrics = getResources().getDisplayMetrics();
mWidth = metrics.widthPixels;
mHieght = (int) getResources().getDimension(R.dimen._height);
Ion.with(imageView).resize(mWidth, mHieght).load(url)
Edit:
This is with android:scaleType="centerCrop"
ImageView has scale type feature. Check ImageView.ScaleType. I think centerInside or fitCenter is what you are looking for.
Update
Sorry didn't realize you have a fixed height at first. centerCrop should be the answer.
If you have a known max size for your images, you can set the width of your layout equal to the largest width of your images set, then take all centered. This worked for me:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="1920px"
android:layout_height="fill_parent"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/your_image" />
</RelativeLayout>
</FrameLayout>
Where 1920px is the largest width of your images set.
The result is acceptable, EDIT: Image is perfectly centered.
I achieved this using "paddingLeft" and "paddingRight" on parent layout; while apply a negative value to padding and margins to the child layout / ImageView. "android:clipToPadding" flag must be false to prevent the clipping.
<LinearLayout
...
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:clipToPadding="false"
...>
<ImageView
android:layout_marginLeft="-10sp"
android:layout_marginRight="-10sp"
android:paddingRight="-10sp"
android:paddingLeft="-10sp"
...>
I am using this ImageLoader Library for my ListView. It by default decodes the file to a 70px bitmap. But then I changed it to 1000, and the image I get is proper. The only problem is displaying the image in the ImageView.
The ImageView in the ListItem is like this:
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="centerInside" />
This displays an image which is centered in the ImageView, with proper aspect ration(no stretching etc..) and has empty spaces on all sides. The empty spaces might be because of the lower dimensions of the image.
I want the image to take complete width of the screen, and maintain the aspect ration. How do I get it?
I tried:
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
But then the image is stretched horizontally and completely out of aspect ration. Looks like its taking the height same as the actual image height but width as fill_parent.
Thank You
Try this:
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/name"
/>
Make android:layout_width="match_parent".
Set height to some arbit but legit value to begin with like android:layout_height="0dp".
And for height, since there is no appropriate scaleType to get the aspect ratio the way you desire (i believe - confirm that developer.android.com/reference/android/widget/…), what you will have to do is in the java file, get the image's width and then set the height according to aspect ratio you need (0.6 in your case).
Try adding this
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
/>
I am developing a sort of ebook application and I am trying to display an image in the following two ways. When the screen is in portrait, I have the image horizontally fitted and vertically centered. When the screen is in landscape, I want the image to fill the screen horizontally with the aspect ratio maintained. Since the images that I am using are longer than they are wide, I have a ScrollView to allow viewing of the entire image.
How should I be scaling my ImageView to achieve this? I know that FIT_CENTER comes close to what I want to do, but since the image is narrower than it is tall, it does not fill the width. I tried FIT_XY with setAdjustViewBounds set to true but that still doesnt maintain my aspect ratio. Does anyone have any ideas?
Here is my code so far:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:isScrollContainer="true"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_gravity="center_vertical|clip_horizontal"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:id="#+id/Root">
<ScrollView android:id="#+id/ScrollView01"
android:tag="scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|clip_horizontal">
<ImageView android:scaleType="fitCenter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imgView"
android:adjustViewBounds="true"></ImageView>
</ScrollView>
</LinearLayout>
http://i.stack.imgur.com/9J8IW.png
This is in portrait. It is exactly as I want it. The image fits the width of the screen and aspect ratio is maintained.
http://i.stack.imgur.com/Kw68o.png
This is in landscape. The image's aspect ratio is maintained and the scrollview works as needed, but I need the imageview to stretch the width of the screen