user387184
user387184

Reputation: 11053

ImageView - how to force scaling the width uniformly having defined the height

I defined two ImageViews and set the background like this:

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

And this is the xml with the Image views:

<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" />

Now all that should happen is that when I set the background of the imageviews, the png adjusts uniformly its size to the parents 100dp height while the width should scale uniformly - down or up.

I tried to set the width smaller and larger. When its set to 300dp for example then it stretches the png width to 300 and 100 height while it should only stretch it's width so far that the height reaches 100.

I thought "fitCenter" would do that. I also tried all the other attribute values, but without luck.

Thanks!

Upvotes: 0

Views: 1597

Answers (1)

youchy
youchy

Reputation: 427

android:scaleType only take care about the android:src why don't you use that param?? do you really want use background and don't set a source?

Maybe yoy can have one RelativeLayout with two ImageView for each imageView, one with the "background scaled" and the der with the real source.

Upvotes: 1

Related Questions