7

Hello everyone and thanks for your help.

I have a project in Android Studio.

I am using an ImageView inside vertical LinearLayout.

Here are my parameters;

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:src="@drawable/myimg"
/>

You can see my image down below (red block) and imageview borders (blue line) there is no problem with width, it is %100 of my layout, but I want my image to fit the area while keeping the aspect ratio. as you can see in image there are white spaces, the ImageView height is bigger than my image, how can I fit image in ImageView?

I want this because I want that image to fit the width even the phone is portrait or landscape.

enter image description here

Liuting
  • 1,098
  • 17
  • 35
yucel
  • 365
  • 4
  • 11
  • Instead of android:src, Use android:background and then check.It may get the required effect as you need. – Ram Sep 23 '14 at 13:00

5 Answers5

27

I found the solution android:adjustViewBounds="true"

Liuting
  • 1,098
  • 17
  • 35
yucel
  • 365
  • 4
  • 11
0

The size of the imageview is correct.You have to scale your images accordingly.ImageView has property of scaling.Try this android:scaleType="fitXY" and let me know.

williamj949
  • 11,166
  • 8
  • 37
  • 51
0

your code is write but i don't know about parent layout and you image background is white & red part is middle in this background.

Here is code with parent layout relative

<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">



<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/red"
     />

enter image description here

Bhagirathsinh Gohil
  • 673
  • 1
  • 9
  • 25
0

For ImageView, android:scaleType="fitXY" will loss the aspectration. Its better to use android:scaleType="fitCenter" and set the background for the imageView transparent.

If you dont want to keep aspectration, then setbackground() or fitxy will do your requirement

Jithu
  • 1,478
  • 1
  • 13
  • 21
0

In imageview, there is a option of center crop -

android:scaleType="centerCrop"

What it will do is that it will crop the image from center. The output will be like -

resized

Confuse
  • 5,646
  • 7
  • 36
  • 58