Display an image with height 'matchparent' and also keep the aspectratio - android

I want to display an image in Imageview with full height and also keep the aspect ratio of the image. To view the remaining part of the image, I want horizontal scrolling on that image. Please share the idea behind it?

As mentioned by ResolutioN, adjustViewBounds does the trick, but fitXY is not necessary. Just wrap your ImageView inside a HorizontalScrollView
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="#drawable/image" />
</HorizontalScrollView>

Combine adjustViewBound and scaleType="fitXY" to achieve your goal. This will keep the aspect ratio of the image. Like this:
<ImageView
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent" />

Hope this code helps you:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/horizontalScrollView"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</HorizontalScrollView>

Related

How to set an xml image resizing to its parent's height

I am doing an application that needs to use images, but the ImageView on the HorizontalScrollView isn't resized to its parent's height. How to do? I tried all, like centerCrop, ScaleType, etc... but nothing worked
<HorizontalScrollView
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="#dimen/scrollShareImgHeight">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sharedLinearLayoutImgs"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/loremipsum"/>
</LinearLayout>
</HorizontalScrollView>
Keep Linearlayout's height as "match_parent".

Scale my item image to fit the screen properly

So I got a problem scaling my image to fit my home screen appropriately.
Here are some screenshots.
Height problem
Width problem
I want to achieve something like this
XML Item code
<?xml version="1.0" encoding="utf-8"?>
<com.inthessaloniki.cityguide.view.SelectorRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_poi_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/selector_clickable_item_bg_inverse">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"/>
</RelativeLayout>
</com.inthessaloniki.cityguide.view.SelectorRelativeLayout>
I want the images to match the parent width but scale their heigth accordingly.
The scaleType="fitXY" forces image to fix to screen and does not keep aspect ratio.
Try this instead
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
In the end I found out a solution.
Here it is in case someone had the same problem.
I used scaleY and padding to fit the images accordingly.
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:scaleType="fitXY"
android:scaleY="1.25"/>

Imageview not matching the width of Linearlayout

I have a layout in which a linearlayout contains imageview as its direct child. I want to fit the width of this imageview so that it spans horizontally to match the size of the parent which will take the width of the whole screen.
How can I do that? This is what i've done so far:
<?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="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="0dp"
android:src="#drawable/policemen_salute"
android:id="#+id/banner"
/>
</LinearLayout>
</RelativeLayout>
It looks like this
Thanks!
Add the 'scaletype' attribute:
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:scaleType="fitXY"
android:layout_margin="0dp"
android:src="#drawable/policemen_salute"
android:id="#+id/banner"/>
See
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
You have to use a scaleType to scale correctly the image.
try your ImageView like this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="0dp"
android:src="#drawable/policemen_salute"
android:id="#+id/banner"
android:scaleType="centerCrop"/>
You can use android Scaletype Property .
android:scaleType="fitXY"
Then set android adjustviewbounds true from XML .Try this way I hope it will helps you.
If you wish to scale your image, you might want to add
android:scaleType="fitXY"
(Scale in X and Y independently, so that src matches dst exactly)
to your ImageView
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="0dp"
android:scaleType="fitXY"
android:src="#drawable/policemen_salute"
android:id="#+id/banner"
/>

ImageView adjustViewBounds not working with fitXY

I use the following code to show my button
(Because I need to add the text on the button, so I will not use the ImageButton)
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/bottom001"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</RelativeLayout>
But the ImageView will have some empty area in the View
I think "fitXY" will fill the View, but it didn't
How can I do in this case?
Thanks for your help :)
set android:layout_width="wrap_content" And Use LinearLayout
Finally
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/test"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
For details please read android-imageview-scaletype-samples
FIT_XY
Scale the image using FILL.Stretch the picture inside the available width and height, disgarding the aspect ratio.

android imageView scale

I have an ImageView which is populated from database (the image comes from database). in the database i have images both portrait and landscape!! how can i adjust the imageView to scale it self according to the image width and height. I have tried many ways but no result! any help please!!!!
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slideshow"
android:orientation="horizontal"
android:padding="0dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginRight="5dp"
android:id="#+id/slide1"
android:src="#drawable/ddd"
/>
Try this code. adjustViewBounds attribute makes the ImageView the same size as image that you put in it.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
If you need specific width or height change the wrap_content value.

Categories

Resources