0

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!

user387184
  • 10,953
  • 12
  • 77
  • 147
  • 1
    have you tried the "centerInside" scale type? – dymmeh Mar 15 '12 at 18:55
  • possible duplicate of [How to scale an Image in ImageView to keep the aspect ratio](http://stackoverflow.com/questions/2521959/how-to-scale-an-image-in-imageview-to-keep-the-aspect-ratio) – Ted Hopp Mar 15 '12 at 18:56
  • ->dymmeh, thanks, but "centerInside" looks exactly the same. ->Ted Hopp, I have an ImageView that needs to be set programmatically with a Drawable id - the other question is similar - but uses xml or setImageBitmap. I need it programmatically, and I don't have the bitmap for the Drawable id. Or do I need to convert from R.drawable.id to bitmap first? – user387184 Mar 15 '12 at 19:11

1 Answers1

1

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.

youchy
  • 427
  • 3
  • 12
  • I even tried setting the Bitmap programmatically, which is supposed to be equivalent to android:src. It still not work correctly. The reason for this seems to be the programmatically manipulation of the content of these ImageViews. I ended up drawing my content to canvas/bitmaps and putting this into one single ImageView – user387184 Mar 15 '12 at 23:48