【问题标题】:Android: How to resize image to fill parent?Android:如何调整图像大小以填充父级?
【发布时间】:2015-07-08 19:26:11
【问题描述】:

我正在开发显示图像的应用程序,我需要创建一个以 1:1 纵横比显示大图像的视图。

首先,我有分辨率为72x72 的缩略图。我已将它们放在ImageView 中,但即使我添加参数android:width="fill_parent"android:height="wrap_content",图像也不会是ImageView 的大小,而是仅居中在中间。

我尝试添加参数android:scaleType="centerCrop",它可以很好地水平调整图像大小,但高度保持不变。

我如何设置我的ImageView,使其在所有设备上的宽度与父视图一样高,也应与高度一样。

我可以以编程方式执行此操作,但我需要能够在 XML 中执行此操作。

我的 XML 文件:

<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>

【问题讨论】:

  • 将图像设置为android:background,它们将被拉伸以匹配父尺寸。

标签: android image android-layout android-xml scaletype


【解决方案1】:

在保持纵横比的同时使图像拉伸到父级宽度

设置

1- android:layout_widthmatch_parent

2- layout_heightwrap_content

3- adjustViewBoundstrue

像这样:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/unknown"
    android:adjustViewBounds="true" />

注意:这可能无法在预览中正确呈现,但可以在运行时使用。

【讨论】:

    【解决方案2】:

    我通常使用android:width="match_parent"android:height="wrap_content",正如你所提到的,但我使用android:scaleType 作为我的ImageView 布局。

    您可以阅读更多关于它的信息here

    【讨论】:

      【解决方案3】:

      用途:

      android:scaleType="fitXY"
      

      【讨论】:

      • 这不会正确调整它的大小
      【解决方案4】:

      我认为您需要将此添加到您的 ImageView

      android:adjustViewBounds="true"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-11
        相关资源
        最近更新 更多