关于ImageView的自适应,一般是根据图片的宽高比进行等比缩放展示。下面重点讲解一下如何正确使用 ImageView的adjustViewBounds属性实现图片的自适应展示。
adjustViewBounds官方API:
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
Must be a boolean value, either "true" or "false".
大致意思是:当adjustViewBounds="ture"的时候,可以通过调整 ImageView的界限来保持图片的宽高比例。
setAdjustViewBounds()源码:
public void setAdjustViewBounds(boolean adjustViewBounds) {
mAdjustViewBounds = adjustViewBounds;
if (adjustViewBounds) {
setScaleType(ScaleType.FIT_CENTER);
}
}
看源码我们知道setAdjustViewBounds(true)设置为true时,会把 ImageView的 scaleType属性设置为fitCenter;在使用时如果同时还在xml文件中设置了android:scaleType,则android:scaleType的优先级会高于android:adjustViewBounds,设置的android:adjustViewBounds="ture"中对 scal