屏幕从垂直转向水平时的背景失真?

时间:2014-02-17 06:55:43

标签: android background background-image relativelayout

在我的片段中,我将背景设置为Relativelayout

但是当屏幕从垂直(纵向)转到水平(横向)时,背景图像看起来像是扭曲的。

无法将scaleType设置为Relativelayout

当屏幕从垂直变为水平时,是否有任何方法可以使背景自动缩放?

(不需要检测屏幕尺寸,只需让背景图像自动缩放而不失真)

提前致谢!

2 个答案:

答案 0 :(得分:1)

Image-view放入Relative Layout并将其用作背景,如下所示:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/myBg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/my_bg_image" >

</RelativeLayout>

通过这种方式,您可以将其他视图放在bg的顶部,并且可以正确缩放。

答案 1 :(得分:0)

如果为纵向模式创建了背景图像,并且如果它涉及图形或纹理,则很明显在横向模式下看到它会失真。< / p>

在这种情况下,您可以使用两个不同的背景图像,而不是使用一个背景图像,一个用于纵向模式,另一个用于横向模式。

创建两个不同的布局文件夹

  1. / RES /布局/
  2. / RES /布局脊/
  3. 将布局xml放在具有不同背景图像的这两个文件夹中

    例如

    1. /res/layout/
    <RelativeLayout 
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="@drawable/background-port">
        <!-- layout code will go here -->
        </RelativeLayout>
    

    第二个布局将具有不同的背景图像(为横向模式创建)

     2. /res/layout-land/
                <RelativeLayout 
                       android:layout_width="match_parent"
                       android:layout_height="match_parent"
                       android:background="@drawable/background-land">
                <!-- layout code will go here -->
                </RelativeLayout>
    

    这不是你问题的直接答案,关于在RelativeLayout中使用scaleType,而是解决问题的标准方法

    此外,如果您的背景图片不包含复杂的图形或纹理,请考虑使用Nine-patch drawables作为背景。在这种情况下,您不必创建如上所述的不同文件夹。请查看This以获取更多信息