我的 xml 中有一个 ImageView -
<ImageView
android:id="@+id/image"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
我正在使用 Glide 库从 url 中获取图像并将其设置到 ImageView 中。
我只想将图像的宽度与父宽度匹配,然后在不影响纵横比的情况下调整图像的高度。
如果图像尺寸非常小,则应将其扩展以匹配父级宽度;如果图像尺寸大于屏幕尺寸,则应将其缩小以匹配父级宽度。
ImageView img = (ImageView) findViewById("image");
Glide.with(this).load("url here").into(img);
我应该使用哪种 scaleType?
是否可以用 XML 或它需要 java 代码?
提前致谢。 :)
最佳答案
从 xml 中是可能的。我使用以下代码实现了它。
<ImageView
android:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
Scale XY 使用 FILL 缩放图像并调整 View 边界以保持纵横比。 我认为您不需要中心水平属性。
在此之后,我调用了一个照片加载器库来将图像下载到 ImageView 中。
关于android - 适合图像宽度以匹配父级并根据 ImageView 中的纵横比调整高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50313020/