ImageView中android:adjustViewBounds属性

本文深入探讨了Android ImageView中的adjustViewBounds属性的作用、使用方法及其与maxWidth、maxHeight的配合使用,帮助开发者更好地调整图片显示比例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public voidsetAdjustViewBounds(boolean adjustViewBounds)

Since: API Level 1

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

Related XML Attributes

Parameters

adjustViewBounds

Whether to adjust the bounds of this view to presrve the original aspect ratio of the drawable

 

该方法是boolean类型的。如果你想让这个ImageView去调整它的界限然后去保存它绘制的范围,你可以把该属性设置为true。

相关的XML属性:

 

android:adjustViewBounds="  "(设置为true或者false)

用Java代码去实现的方法是:

ImageView    iv=new ImageView(this);

iv.setAdajustViewBounds( boolean b );(b 可以设置为true或者false)

 

 

参数:

 

adjustViewBounds: 是否去调整这个视图的界限去保存绘制的初始范围比例;

 

 

上面是Android API中说的。我百度了一下,得出如下结果:

android:adjustViewBounds 是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。

该句话的后半句我还没有去验证,但我用的时候没有与maxWidth、MaxHeight一起使用。

shu3.xml:<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background" tools:context=".ShuHai3Activity"> <!-- Toolbar --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 播放视频区域 --> <!-- 时间线和缩略图区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintHeight_percent="0.55" android:background="@android:color/black" app:layout_constraintBottom_toTopOf="@id/thumbnail_scrollview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.6" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintVertical_bias="0.0"> <!-- 模拟萤石播放加载动画 --> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <!-- 视频播放View,可以替换成具体的视频播放控件 --> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintHeight_percent="0.45" android:fillViewport="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/video_container"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:columnCount="2" app:rowCount="0" app:alignmentMode="alignBounds" app:useDefaultMargins="true" android:padding="8dp"> <!-- 多个缩略图子项 --> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> </android.support.v7.widget.GridLayout> </LinearLayout> </ScrollView> </android.support.constraint.ConstraintLayout> 修改下面shuhaisp.xml布局和上面shu3.xml布局一样,保持shuhaisp.xml依赖不变必须使用android支持库。shuhaisp.xml:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- tools:context=".ShuhaiSp"--> <!-- <!– 顶部工具栏 –>--> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#44bd32"> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 视频播放区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@id/toolbar" android:layout_above="@+id/thumbnail_scrollview" android:layout_marginTop="0dp" android:background="@android:color/black"> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <!-- 缩略图区域 --> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_alignParentBottom="true" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="2" android:rowCount="0" android:alignmentMode="alignBounds" android:useDefaultMargins="true" android:padding="8dp"> <!-- 缩略图示例 --> <!-- <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" /> --> </android.widget.GridLayout> </LinearLayout> </ScrollView> </RelativeLayout>
最新发布
06-25
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值