【问题标题】:android:background stretching not preserving aspect ratioandroid:背景拉伸不保留纵横比
【发布时间】:2014-07-02 09:43:59
【问题描述】:

在横向模式下,我的背景图片会拉伸,我希望图片放大而不是拉伸以适应尺寸。从我所见,我必须将其添加到它自己的布局中(例如LinearLayout)或“ImageView”,并且在视图上具有内容的布局,我这样说是否正确?

我当前的 xml 布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/instructions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/photo_background"
android:tag="layout" >

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fadingEdge="none" >
    <include layout="@layout/instructions_internal" />
</ScrollView>

其中包括以下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:id="@+id/instructionsLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/transparent_black" >

    <RelativeLayout
        android:id="@+id/more_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <WebView
            android:id="@+id/top_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" />

        <Button
            android:id="@+id/test"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/top_content"
            android:layout_marginBottom="10dp"
            android:layout_centerHorizontal="true"
            android:onClick="onClickHandler" />

        <Button
            android:id="@+id/instructionsbutton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/test"
            android:background="@color/dark_gray"
            android:drawableLeft="@drawable/arrow_down_float"
            android:gravity="left|center"
            android:onClick="onMoreInstructions"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:text="@string/more_instructions_start"
            android:textColor="@color/solid_white" />
    </RelativeLayout>

    <WebView
        android:id="@+id/bottom_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/more_info"
        android:layout_marginTop="10dp" />
</RelativeLayout>

我们将不胜感激任何有关这方面的指导。谢谢

【问题讨论】:

  • 因此,不要使用容器的 background 属性,而是使用 ImageView (match_parent, match_parent) src 属性。并将 scaleType 设置为 FIT_XY
  • 您已将背景图片放在所有可绘制文件夹上,也用于横向定位
  • @DerGolem 实际上,我相信FIT_XY 会做同样的事情。 OP 正在寻找 CENTER_CROPCENTER_INSIDE
  • @IvanBartsov:你说得对,赶时间(午餐时间),但概念仍然存在:使用 ImageView 的 src 而不是容器的背景。

标签: android android-layout background scaling


【解决方案1】:

使用匹配或符合 android 设备分辨率和屏幕尺寸的图像。

图像本身不能放大,即使你使用专门的线性布局,图像也必然会被拉伸。

将所有可能分辨率的图像保存在您计划在其上运行应用程序的单独文件夹中。

【讨论】:

  • 有很多设备和屏幕尺寸,我必须拥有 1000 个相同图像的副本才能在所有设备上看起来都很好
  • 您无法预测所有设备分辨率的分辨率。最好针对您想要的外观进行编程。
【解决方案2】:

是的,你是对的。 View's background 缩放类型相当于ImageViewScaleType.FIT_XY 除非您使用wrap_content 并且内容不会使其超出背景边界,否则它会弄乱纵横比。

所以在这种情况下,我通常使用ImageView 放置在需要适当缩放背景的视图后面。 (很可能您正在寻找CENTER_CROP 比例类型)

UPD这是一些代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/instructions"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="layout" >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/photo_background"
        android:scaleType="centerCrop" />

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fadingEdge="none" >
        <include layout="@layout/instructions_internal" />
    </ScrollView>
</FrameLayout>

【讨论】:

  • 你能举个例子吗?
  • 非常感谢您的更新!那你为什么要在FrameLayout 中这样做呢?只是出于兴趣。
  • @Paddy1990 FrameLayout 的布局逻辑比RelativeLayout 简单得多。因此,CPU 花费的周期更少。无论如何,您都没有使用任何RelativeLayout 属性,因此FrameLayout 是这里的合乎逻辑的选择。另外,如果对您有帮助,请不要忘记接受我的回答:)
  • 我知道我从来没有新的,每天学习新的东西。无论如何,非常感谢您的帮助,非常感谢。如果没有这个站点,我将无法使用该应用程序。再次感谢!
猜你喜欢
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多