0

I'm working on ImageView widget and it seems me that both attributes (android:adjustViewBounds, layout_width and layout_height together) has the same affect on the result i get. I'm asking cause maybe i'm using an image that isn't suit to emphasize the difference. First xml Code (with android:adjustViewBounds set to true, layout_widht and layout_height also sets to wrap_content since i must give them a value):

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"><TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:textSize="24sp"
    android:text="TextView" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:adjustViewBounds="true"
    android:src="@drawable/image_1" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="TextView" />

Second xml code (the same code only without the line android:adjustViewBounds=true ):

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:textSize="24sp"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/image_1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="TextView" />

</LinearLayout>
Eitanos30
  • 1,331
  • 11
  • 19

1 Answers1

0
  • android:adjustViewBounds set to true is setScaleType(setScaleType(ScaleType.FIT_CENTER)) enter image description here

With fit_center: Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst. enter image description here

Cuong Nguyen
  • 970
  • 6
  • 17
  • @Curong Nguyen, Thanks! But can you please refer to the the following issues: 1. Does layout_witdh and layout_height that are set to "true" aren't affect the result we see at all? 2. In the following link: https://stackoverflow.com/questions/2521959/how-to-scale-an-image-in-imageview-to-keep-the-aspect-ratio, you can see that the answer that was approves says that *"You should also see android:adjustViewBounds to make the ImageView resize itself to fit the rescaled image"*. Doesn't it mean that the image will be placed "as is", and the imageView will be fit to the image? – Eitanos30 Dec 24 '19 at 08:56