ImageView - 如何在定义高度的同时强制缩放宽度

时间:2012-03-15 18:51:22

标签: android imageview

我定义了两个ImageView并设置了这样的背景:

  int tmpID = findViewById(R.id.mypng)
  ImageView   tmpIV.setBackgroundResource(tmpID);

这是带有图像视图的xml:

<RelativeLayout
        android:id="@+id/myDisplay"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp" >

        <ImageView
            android:id="@+id/b1"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:contentDescription="@string/anyStringValue"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/b2"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_toRightOf="@+id/b1"
            android:contentDescription="@string/anyStringValue"
            android:scaleType="fitCenter" />

现在应该发生的是,当我设置图像视图的背景时,png将其大小均匀调整为父级100dp高度,而宽度应均匀缩放 - 向下或向上。

我试图将宽度设置得越来越小。例如,当它设置为300dp然后它将png宽度拉伸到300和100高度时,它应该只拉伸它的宽度到目前为止高度达到100.

我认为“fitCenter”会这样做。我也尝试了所有其他属性值,但没有运气。

谢谢!

1 个答案:

答案 0 :(得分:1)

android:scaleType只关注android:src你为什么不使用那个参数?你真的想要使用背景而不设置源吗?

也许yoy可以为每个imageView设置一个RelativeLayout和两个ImageView,一个带有“background scaled”,另一个带有真实源。