Android之使ImageView中图片保持比例显示,且不超过最大高度和最大宽度限制

1、原来的,没有大小尺寸限制,不重要

<ImageView android:id="@+id/img_one_show" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:scaleType="fitStart" 
android:visibility="visible" 
android:src="@drawable/capture_photo_icon" 
android:layout_marginEnd="10dp" 
android:layout_marginTop="10dp"
 android:maxHeight="250dp" android:maxWidth="200dp"/>

2、修改后的ImageView

最大宽度和最大高度限制的dp值可根据需要设置;
要确保图片适应宽度或高度完整显示,同时限制其最大高度为250dp、最大宽度为200dp,并保持比例不变,您需要对 ImageView 的属性进行如下调整:

<ImageView
    android:id="@+id/img_one_show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:visibility="visible"
    android:src="@drawable/capture_photo_icon"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:maxHeight="250dp"
    android:maxWidth="200dp"/>

3、主要改动如下:

将 android:scaleType 从 fitStart 改为 fitCenter: fitCenter 会将图片等比例缩放至图片的宽度或高度至少与 ImageView 的一个维度相匹配,然后将图片居中放置在 ImageView 内。这样既能保证图片内容完整显示,又能保持图片原始比例

添加 android:adjustViewBounds=“true”: 这个属性会让 ImageView 自动调整其边框以适应图片内容的尺寸,确保图片按比例缩放时不会被剪裁,并且始终保持设定的最大高度和宽度限制

通过这些更改,您的 ImageView 将会以保持原图比例的方式显示图片,同时确保图片的高度不超过 250dp,宽度不超过 200dp。图片会根据自身的长宽比自动调整,以适应这两个最大尺寸限制,并通过 fitCenter 居中显示在 ImageView 中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值