Android: How to resize image to fill parent?
Asked Answered
P

4

7

I am working on application which shows images, and I need to create a view which shows big images in a 1:1 aspect ratio.

First, I have thumbnails which are at a 72x72 resolution. I have put them in an ImageView but even if I add the parameters android:width="fill_parent" and android:height="wrap_content", the image would not be not the size of the ImageView but is only centered in the mid.

I tried adding the parameter android:scaleType="centerCrop" which resized the image well horizontally but the height remained the same.

How could I set up my ImageView so that it would be on all devices as width as the parent view and it should also be as height.

I can do this programatically, but I need to be able to do it in XML.

My XML File:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    // The ImageView I am talking about above
    <ImageView
        android:id="@+id/invitation_imageview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:scaleType="centerCrop"
        android:src="@drawable/face_woman_smile" />

    <ImageView
        android:id="@+id/invitation_avatar_view_1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:src="@drawable/face_woman_smile"
        android:scaleType="center"
        android:layout_margin="1dp" />

</LinearLayout>
Pincince answered 29/4, 2015 at 13:16 Comment(1)
set the images as android:background and they will be stretched to match the parent size.Asur
D
12

I think you need to add this to your ImageView

android:adjustViewBounds="true"
Decapolis answered 29/4, 2015 at 13:20 Comment(0)
A
4

Use:

android:scaleType="fitXY"
Achromaticity answered 29/4, 2015 at 13:26 Comment(1)
this will not resize it properlyPincince
K
3

To make image stretch to parent's width while keeping aspect ratio

Set

1- android:layout_width to match_parent

2- layout_height to wrap_content

3- adjustViewBounds to true

Like this:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/unknown"
    android:adjustViewBounds="true" />

Note: This might not render correctly in Preview but would work on runtime.

Kultur answered 29/4, 2015 at 13:43 Comment(0)
O
1

I usually use android:width="match_parent" and android:height="wrap_content", as you mentioned, but I use android:scaleType for my ImageView layouts.

You can read more about it here.

Orosco answered 29/4, 2015 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.