Can't manage to resize image and keep the ratio - android

My goal is simple : have a 16/9 ratio image in an imageView. I use a png image which dimensions are 16x9. 2 possibilities for my app :
portrait : width fixed, has to scale height
landscape : height fixed, has to scale width
The first one works fine with this layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/welcomeRootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/welcomePilotingFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#null"
android:scaleType="fitXY"
android:src="#drawable/black_16_9e" />
</FrameLayout>
<!-- Other stuff that aren't relevant -->
</RelativeLayout>
This gives me what i want : my red cornered image is resized
Now the fun part begins : landscape: Here's my layout (symetric off portrait) :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/welcomeLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" >
<FrameLayout
android:id="#+id/welcomePilotingFrame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ff00ff00" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="#null"
android:scaleType="fitXY"
android:src="#drawable/black_16_9e_border" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/welcomePilotingFrame" />
</RelativeLayout>
<!-- Other stuff that aren't relevant -->
</LinearLayout>
And now, the imageView understands it has to be all its parent high, but does't resize its width and so doesn't keep my 16/9 ratio !
Am I missing something ?

Change the layout_width of second framelayout to match_parent. Also scale-type = fitxy changes aspect ratio of the image. Change it to FIT_START

Related

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"/>

NetworkImageView to occupy full width of screen

I am using com.android.volley.toolbox.NetworkImageView.
Image is coming from url.
I want to display image so that its width to occupy full width of screen not height because below image there is a caption which is a Textview.
Here is the activity's xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Here is the items xml.
<?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"
android:gravity="center" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/photo_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp"
android:src="#drawable/button_register" />
<TextView
android:id="#+id/caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/photo_image"
android:layout_marginLeft="15dp"
android:text="Check"
android:textColor="#color/white" />
</RelativeLayout>
From the backend the image size is 300 X 200 pixels.
How to set imageview so that it can occupy full width of the screen.
Can anyone help?
NetworkImageView extends ImageView so you should be able to use everything that works with ImageView.
So, if you want your image to occupy full width and adjust its height so it maintains its aspect ratio (at least that's what I understood you wanted to do) use the adjustViewBounds property
Your xml will look like this:
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/photo_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp"
android:src="#drawable/button_register"
<!-- add adjustViewBounds -->
android:adjustViewBounds="true" />

why the image keep a white space from the bottom and the top?

what i have is an activity and i add an images to it, and i make it as background for the activity with the following xml :
<LinearLayout 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"
tools:context=".SplashScreen">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/splash5" />
</LinearLayout>
and the image size is : 550 px for width and 800 px for height
and as you can see from the image you can see a white space in top and in bottom .. why this is happening even it's fill parent for width and height?
use like this:
<LinearLayout 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"
tools:context=".SplashScreen">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/splash5"
android:scaleType="fitXY"
/>
</LinearLayout>

how to set layout wrap scaled content

my code is like below,when the FrameLayout layout_height from
android:layout_height="wrap_content"
to
android:layout_height="120dp"
the layout becomes
i hope the image's height will become 120dp, and keep the scale.
And the width of the layout will be followed by changes , as described wrap_content ,
but it only seems to adapt to the size of the original image .
what can i do?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_thumbnail_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/white" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/network_3" />
</FrameLayout>
How about specifying both the layout_width and layout_height to 120dp each? Like below
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_thumbnail_container"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:background="#color/black" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher" />
</FrameLayout>

android layout large image

I have a large image myImage.png (its dims are 1536 x 784):
I want this image to be placed at the bottom of my layout scaled uniformly and fitting the width:
Layout is
<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="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/myImage"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter" >
</ImageView>
</RelativeLayout>
I've tried dozens of combinations for layout_width, layout_height, adjustViewBounds and scaleType and nothing helped: it always looks as follows:
Help me please.
As someone says, use src instead of background:
<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="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/myImage"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter" >
</ImageView>
</RelativeLayout>
You are using an ImageView. ImageViews use the src attribute to indicate which image to use and to apply the scaleType to.
background, is, as name suggests, merely a background attribute, made to be displayed on that whole background thing.
try this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/myImage"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"
android:scaleType="fitcenter" >
</ImageView>

Categories

Resources