Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android adjustViewBounds bug?

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"/>
like image 720
Frank Avatar asked Dec 04 '22 16:12

Frank


1 Answers

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

like image 180
devunwired Avatar answered Dec 21 '22 03:12

devunwired