ImageView adjustViewBounds属性

ImageView设置属性android:adjustViewBounds=“true”,用于保持显示的图片的宽高比例不变,即:当宽度/高度变化之后,高度/宽度根据图片本身的宽高比而变化。

网上关于ImageView adjustViewBounds属性的文章不少,很多都说android:adjustViewBounds="true"属性要结合maxWidth和maxHeight一起使用。实践是检验真理的唯一标准,本猿特意用demo测试了下,发现并非如此
在这里插入图片描述
测试代码如下:
在这里插入图片描述

//布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    
    <ImageView
        android:id="@+id/iv"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/test_drawable"
        android:background="@color/colorAccent"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:textAllCaps="false"
        android:text="width+5"/>

</RelativeLayout>

//代码
//点击Button,width增加5。

mImageView = findViewById(R.id.iv);
mButton = findViewById(R.id.btn);
mButton.setOnClickListener(this);

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn:
            changeWidth();
            break;
        default:
            break;
    }
}

private void changeWidth() {
    ViewGroup.LayoutParams layoutParams = mImageView.getLayoutParams();
    layoutParams.width += 5;
    mImageView.setLayoutParams(layoutParams);
}

未点击button时的效果:
在这里插入图片描述

多次点击button之后的效果:
在这里插入图片描述

注意:

如果在ImageView的布局设置固定宽高,则变化无效。

亲测宽高固定数值:

android:layout_width="60dp"
android:layout_height="40dp"

多次点击后:
在这里插入图片描述

如有不周之处,欢迎评论区指出哦。周末愉快~
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值