android - 如何缩放 ImageView 宽度

标签 android android-layout android-imageview

这是我的布局文件

<LinearLayout ...
<ImageView
    android:id="@+id/feed_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:adjustViewBounds="true"
    android:contentDescription="@string/image_content_description" />

但是 ImageView 宽度不匹配父 宽度。 (宽度为图源宽度)

Image Source loaded Lazy-loading from url.

如何缩放 ImageView 的宽度,无论图像源如何?

我要

宽度 = 匹配(或填充)父元素。

高度 = 自动缩放

最佳答案

您可以通过两种方式实现这一目标:

  1. 如果您的图像比 ImageView 大,您只能使用 xml

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"/>
    
  2. 如果您的图像小于 ImageView

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"/>
    

对于第二个选项,您必须测量图像的宽度和高度,并根据 ImageView 的实际宽度(相同比例)在代码中设置 ImageView 的高度。

关于android - 如何缩放 ImageView 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17739094/

相关文章:

java - Android ListView 性能

Android Studio 多个 dex 文件 gradle 错误

android - 我们可以从屏幕中间的某个地方打开滑动抽屉吗?

android - 如何在android中的imageview中从url网站获取图像

java - Android 无法使用 attrs 值扩充布局文件

java - 如何在作为 xml 布局文件一部分的框架布局中创建 Canvas

java - tablelayout中imageview的对齐方式

java - 访问 Android Spinner 中显示的任何子项的方法是什么,而不是仅仅等待 onItemSelected() 事件?

android - 如何将第一个Activity的ImageView共享并保存到第三个Activity

java - 显示有动画问题的 ImagesViews