【问题标题】:Does android:scaleType="fitCenter" only work with fix Layout_width and Layout_height attributes?android:scaleType="fitCenter" 是否仅适用于修复 Layout_width 和 Layout_height 属性?
【发布时间】:2011-04-16 15:57:38
【问题描述】:

我使用Layout_width="fill_parent"和Layout_height="wrap content"。如果图像大于ImageView,它将完美缩小。

但是,我从来没有使用它来放大较小的图像。我尝试了ScaleType 和“AdjustViewBounds”的任意组合:它始终在图像视图的中间保持自己的大小。这是代码...

 <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_height="wrap_content"
        android:background="@drawable/content_bg"
        android:orientation="vertical"
        android:padding="5dp"
        android:layout_width="fill_parent"
        android:layout_marginLeft="1px">
        <ImageView
          android:layout_height="wrap_content"
          android:src="@drawable/test"
          android:id="@+id/ImageViewTest"
          android:layout_width="fill_parent"
          android:adjustViewBounds="true"
          android:scaleType="center"></ImageView>
</LinearLayout>

【问题讨论】:

  • 尝试使用 CENTER_INSIDE 或 FIT_XY。
  • 我都试过了。它们都不适合我。
  • 我发现让它工作的唯一方法是将 layout_hight 和 layout_width 设置为固定值,例如 150dp x 200dp
  • 我无法再测试给定的答案,因为我停止开发 Android 应用程序。请建议我是否应该将答案标记为完成,如果您已对其进行测试并符合我的问题的要求。

标签: android imageview android-layout android-image


【解决方案1】:

我刚刚找到了让它工作的方法。

这适用于 2.1(AVD)和 4.1.2(设备)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter" />

</LinearLayout>

【讨论】:

  • 使用这种方法,如果你在imageView上有边框,那么图片没有缩放到的边上就会有空白
【解决方案2】:
<LinearLayout
    android:layout_height="wrap_content"

使用里面图片的原始尺寸。

尝试更大的或填充父项

【讨论】:

  • 这只是我的黄金答案之一,您不知道我在使用 imageview 及其错误(或被误解)的比例类型时遇到了多少麻烦。泰米尔斯一吨。
【解决方案3】:

使用android:scaleType="FIT_START"android:scaleType="FIT_END" 计算将保持原始src 纵横比的比例,但也将确保src 完全适合dst(来自GoogleDev)。

更多信息:

http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

【讨论】:

  • 它是 android:scaleType="fitStart" 或 android:scaleType="fitEnd",而且这些几乎与 android:scaleType="fitCenter" 完全相同。不同之处在于它们如何处理空白空间,对齐开始、结束或居中。
【解决方案4】:

如果尺寸很小,将android:scaleType="fitXY" 添加到您的 ImageView 应该会拉伸图像。

【讨论】:

  • 是的,如果图像较大,它可以正常工作,但如果图像较小,则它只适合高度或宽度。 Android 处理图片的方式很愚蠢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 2012-10-30
  • 2015-04-07
  • 2016-05-19
相关资源
最近更新 更多