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

1 Answer 1

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

1
  • 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 4, 2014 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.