首页 > 解决方案 > Android图像根据设备屏幕尺寸更改图像尺寸

问题描述

我有一个imageView谁的宽度"match_parent"(每端都有一些边距)。

由于不同的设备具有不同的宽度,因此图像不会填充 imageView,而是会在每一端留下一些间隙,具体取决于我在哪个设备上查看它。

我已经尝试过android:adjustViewBounds="true",但这会拉伸图像以填充严重扭曲内容的宽度。

什么是正确的方法?我应该给android多个不同尺寸的图像副本(比如不同像素密度的多个可绘制文件夹)吗?

更新

尝试将 scaleType 设置为“fitXY”,但这仍在拉伸图像而不保持纵横比。

这是我的xml:

<ImageView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:scaleType="fitXY"
        android:src="@drawable/inner_map"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header" />

如果这很重要,我已将该图像的不同分辨率的多个副本插入到不同的可绘制文件夹中

标签: androidimageviewandroid-drawable

解决方案


<ImageView
    android:id="@id/img"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop" />

https://developer.android.com/reference/android/widget/ImageView.ScaleType


推荐阅读