在Android中以相同的比例缩放背景图像和前景图像

时间:2013-07-18 18:14:22

标签: android background image-scaling foreground aspect-ratio

我有一个居中的背景图片,其中包含我使用ImageView显示的图案。我希望它能填充所有不同屏幕尺寸/密度的宽度和高度,但要保持其纵横比。我想我已经找到了那个部分,但后来我有一个前景图像(在ToggleButton中),它更小并且居中,需要以与背景图像相同的比例缩放。这很重要,因为按钮图像具有需要与背景完美对齐的图形元素。如何正确缩放?这是我到目前为止所得到的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relmain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="centerCrop"
    android:layout_centerInParent="true"
    android:contentDescription="@string/bg_desc"
    android:src="@drawable/bg_img" />

<ToggleButton
    android:id="@+id/button"
    android:background="@drawable/btn_on"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="center"
    android:layout_centerInParent="true"
    android:textOff=""
    android:textOn=""
    android:onClick="toggleLight" />

</RelativeLayout>

谢谢你看看!

1 个答案:

答案 0 :(得分:0)

我认为你应该根据我的技能这样做

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_centerInParent="true"
android:contentDescription="@string/bg_desc"
android:background="@drawable/bg_img" />

可能看起来像你想要的那样。