Android: how to resize image to fill parent?

I am working on an application that displays images, and I need to create a view that displays large images in a 1: 1 aspect ratio.

Firstly, I have resolution thumbnails 72x72. I placed them in ImageView, but even if I add parameters android:width="fill_parent"and android:height="wrap_content", the image will not be size ImageView, but only in the middle.

I tried to add a parameter android:scaleType="centerCrop"that resized the image horizontally, but the height remained the same.

How can I configure mine ImageViewso that it is on all devices as width, as parent, and it should also be as height.

I can do this programmatically, but I need to be able to do this 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>
+4
source share
4 answers

I think you need to add this to your ImageView

android:adjustViewBounds="true"
+6
source

To stretch the image to the width of the parent while maintaining the image format

Set

1- android:layout_widthtomatch_parent

2- layout_heighttowrap_content

3- adjustViewBoundstotrue

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 may not display correctly in Preview, but will work at runtime.

+2
source

:

android:scaleType="fitXY"
+1

android:width="match_parent" android:height="wrap_content", , android:scaleType ImageView.

.

+1
source

All Articles