【问题标题】:ImageView full width and variable height while maintaining aspect ratioImageView 全宽和可变高度,同时保持纵横比
【发布时间】:2017-03-22 07:11:18
【问题描述】:

图像的尺寸可能比设备大。我想缩放图像,使其始终采用全宽和可变长度的 height ,同时保持纵横比。

我还想在滚动时设置Imageview 的特定最小高度。在指定的最小高度之后ImageView 应该被固定。

 <android.support.design.widget.AppBarLayout
        android:id="@+id/foo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/grey_dark_transparent">

        <!--
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/grey_dark_transparent"
            app:layout_collapseMode="parallax"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            -->
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/wallpaper_banner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:src="@drawable/wallpaper_placeholder" />
        </android.support.design.widget.CollapsingToolbarLayout>

        <!-- @Banner -->

    </android.support.design.widget.AppBarLayout>

目前它不保持纵横比。

【问题讨论】:

  • 试试这个,stackoverflow.com/a/42693248/5471104,这里的高度等于宽度,你可以计算纵横比并相应地重新初始化高度。但另一方面,由于您使用的是折叠工具栏,请使宽度和高度与父级匹配。这不会破坏应用的整体外观。
  • @Rico 我期待可以通过 XML 单独完成的事情
  • 哦,我明白了,我几乎不使用 xml ,所以不敢使用它们 :(
  • 对不起@Varuog,我刚刚检查过,当您有预定义的纵横比时,即使 PercentRelativeLayout 也有帮助。动态更改仅通过代码起作用。您可能可以创建一个自定义 ImageView 来处理视图中的这些更改

标签: android imageview android-glide


【解决方案1】:

使用 ScaleType android:scaleType="centerInside" 如下代码。它总是处理图像纵横比。

  <ImageView
        android:id="@+id/wallpaper_banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        android:src="@drawable/wallpaper_placeholder" />

【讨论】:

  • 你确定高度应该是wrap_content而不是match_parent吗?
  • 你交叉检查了吗?它会通过忽略纵横比来获取图像的高度和父级的宽度
  • 但它不应该采用父级的宽度,它应该按照 OP 采用图像的宽度
  • @Rico,在上面的xml中宽度是match_parent
  • @Chetan 不幸的是,图像的宽度被拉伸了。有什么帮助吗?
猜你喜欢
  • 2016-01-17
  • 1970-01-01
  • 2021-06-23
  • 2014-07-01
  • 2016-04-01
  • 2012-06-05
  • 1970-01-01
  • 2021-02-09
  • 2011-04-14
相关资源
最近更新 更多