在不改变宽高比的情况下将背景图像填充到屏幕

时间:2013-03-10 16:59:32

标签: java android

我想将图片显示为我的活动的背景,但是想要在不改变图像宽高比的情况下将其填充到屏幕上(类似this)。

我尝试缩放图像,但这似乎不起作用(因为我使用图像作为背景,没有在图像视图中定义它,或者似乎是问题)。

我该怎么做呢?我甚至尝试在自己的xml文件中定义背景图像,但仍然无效。

2 个答案:

答案 0 :(得分:4)

如果您使用RelativeLayout,您可以放置​​一个ImageView,而不是填充所有布局,然后将其余部分放在上面。这样的事情:

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

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    // Your full layout
</LinearLayout>

</RelativeLayout>

答案 1 :(得分:0)

AFAIK唯一的解决方案是您提到的ImageView。为什么它不适合你?您还可以使用Bitmap.createBitmap()方法调整位图大小,使其达到所需的大小。

相关问题