首页 > 解决方案 > imageView 属性 android:adjustViewBounds=true 是否具有与 layout_width=true 和 layout_height=true 完全相同的影响?

问题描述

我正在研究ImageView小部件,看来我这两个属性(android:adjustViewBoundslayout_widthlayout_height一起)对我得到的结果有相同的影响。我在问,因为也许我正在使用不适合强调差异的图像。第一个 xml 代码(android:adjustViewBounds设置为 true,layout_widhtlayout_height也设置为 wrap_content,因为我必须给它们一个值):

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

第二个 xml 代码(相同的代码只是没有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>

标签: androidimageviewscale

解决方案


  • android:adjustViewBounds 设置为 true 是 setScaleType(setScaleType(ScaleType.FIT_CENTER)) 在此处输入图像描述

使用 fit_center:计算将保持原始 src 纵横比的比例,但也将确保 src 完全适合 dst。至少一个轴(X 或 Y)将完全适合。结果以 dst 为中心。 在此处输入图像描述


推荐阅读