4

When I use android:adjustViewBounds="true" in my imageView xml, it does not function. If I put setAdjustViewBounds(true); in the constructors of my ImageView, it works fine. What is the difference, or is this a bug?

<ImageView
                android:id="@+id/PageViewMainImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="matrix"     
                android:adjustViewBounds="true"               
                android:visibility="invisible"/>
Frank
  • 12,010
  • 8
  • 61
  • 78
  • See this SO Thread it may help you http://stackoverflow.com/questions/7719617/imageview-adjustviewbounds-not-working – K_Anas Jun 06 '12 at 15:40

1 Answers1

16

The order of operations in the ImageView constructor (see source here) parses out and sets the adjustViewBounds property from XML before it parses and sets the scaleType attribute from XML. Part of the setAdjustViewBounds() method (which is called by the constructor with your XML attribute value) is to set the ScaleType to FIT_CENTER when the value is true.

So when the XML you posted is loaded, the ScaleType is first set to FIT_CENTER and then re-set to MATRIX afterwards, all inside the constructor. Compare this with your example of calling setAdjustViewBounds() in Java instead, and now the final ScaleType will be FIT_CENTER as your Java call will happen after the XML attributes are parsed (effectively meaning that your android:scaleType="matrix" attribute is ignored. That difference is likely what you are seeing between "works" and "doesn't work".

I'm not sure if Google would call it a bug, as they are only setting the ScaleType to what they think you want as a convenience to preserve the aspect, but still allow you to modify this. The bounds of the view themselves will still change as the property name directs, the image just may clip in a way you didn't expect.

The docs could probably be more clear on this "feature" though, so you could file a bug report at http://b.android.com

HTH

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • 4
    AdjustViewBounds won't scale up past the natural size of the bmp. If your bmp is 50x50 that is the most you can get out of it, you won't get a scaled image in a 200x200 window. To fix this you need to provide a bmp larger then the window so it can scale down. – thedsz Jun 04 '14 at 14:17