android - 相应地填充宽度和缩放高度 ImageView Android

标签 android android-layout imageview

我有一个用作按钮的 ImageView,我希望它缩放到设备宽度,然后缩放高度以保持纵横比。我一直在关注这个话题:How to scale an Image in ImageView to keep the aspect ratio并尝试了一切,但没有任何效果。

这是我的 XML:

 <ImageView
        android:id="@+id/th_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/towing_button"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="50dp"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:fitsSystemWindows="true"
        android:scaleType="fitCenter"
        android:src="@drawable/call_th_button_selector" />

奇怪的是,它在 Eclipse 的预览中工作正常,但当我在我的设备上测试时,它不会缩放高度。

有人知道解决这个问题的方法吗?

最佳答案

我实际上为此制作了一个小部件。您基本上只是将 ImageView 放在 RatioRelativeLayout 中(这个小部件:https://github.com/elimirks/RatioRelativeLayout)。

示例在github页面上,但我也将其粘贴在这里:

<my.package.RatioRelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    heightRatio="0.75"
>
    <View
        android:layout_width="25dp"
        android:layout_height="match_parent"
        android:background="#f00"
    />
</my.package.RatioRelativeLayout>

您还可以使用 setMaxHeight 设置允许的最大高度。在我的应用程序中,我使用它在横向模式下查看照片,这样图像的高度就不会超过设备的宽度。

关于android - 相应地填充宽度和缩放高度 ImageView Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21334105/

相关文章:

android - 警告:API 'variant.getMergeResources()'已作废,并已替换为 'variant.getMergeResourcesProvider()'

从单独的类调用时 Android ondraw 上下文问题

安卓;如何在一个 Activity 中初始化状态,然后进行另一项刷新?

android - 使用 ActionBarSherlock Menus 在新的 4.0 及更高版本上显示旧菜单按钮 - 包含图片

android - 如何允许 View 超出设备尺寸 - android

android - 图像在 ImageView 中未正确显示

java - 如何计算在 TextView 中看到的字母数量?

android - 更改 AlertDialog 的标题标题

java - android - 将图像保存到画廊

android - 单击动画上的事件 ImageView 仅在开始位置触发