android adjustViewBounds bug?

别说谁变了你拦得住时间么 提交于 2019-12-20 01:14:29

问题


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:


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



来源:https://stackoverflow.com/questions/10917388/android-adjustviewbounds-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!