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"/>
The order of operations in the
ImageViewconstructor (see source here) parses out and sets theadjustViewBoundsproperty from XML before it parses and sets thescaleTypeattribute from XML. Part of thesetAdjustViewBounds()method (which is called by the constructor with your XML attribute value) is to set theScaleTypetoFIT_CENTERwhen the value is true.So when the XML you posted is loaded, the
ScaleTypeis first set toFIT_CENTERand then re-set toMATRIXafterwards, all inside the constructor. Compare this with your example of callingsetAdjustViewBounds()in Java instead, and now the finalScaleTypewill beFIT_CENTERas your Java call will happen after the XML attributes are parsed (effectively meaning that yourandroid: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
ScaleTypeto 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