【问题标题】:Does imageView attribute android:adjustViewBounds=true has exactly the same affect as: layout_width=true and layout_height=true?imageView 属性 android:adjustViewBounds=true 是否具有与 layout_width=true 和 layout_height=true 完全相同的影响?
【发布时间】:2019-12-24 10:20:59
【问题描述】:

我正在开发 ImageView 小部件,我认为这两个属性(android:adjustViewBoundslayout_widthlayout_height together) 对我得到的结果有同样的影响。 我在问,因为也许我使用的图像不适合强调差异。 第一个 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>

【问题讨论】:

    标签: android imageview scale


    【解决方案1】:
    • android:adjustViewBounds 设置为 true 是 setScaleType(setScaleType(ScaleType.FIT_CENTER))

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

    【讨论】:

    • @Curong Nguyen,谢谢!但是能否请您参考以下问题: 1. layout_witdh 和 layout_height 设置为“true”是否完全不影响我们看到的结果? 2. 在以下链接中:stackoverflow.com/questions/2521959/…,您可以看到批准的答案是 “您还应该看到 android:adjustViewBounds 以使 ImageView 调整自身大小以适应重新缩放的图像”。不是说图片会“按原样”放置,imageView会适合图片吗?
    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 2016-03-27
    • 2015-05-29
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 2014-07-27
    相关资源
    最近更新 更多