天天看点

Android中ImageView的ScaleType设置

近日发现网上好多关于ScaleType的说明都是错误的,因此根据官方文档和实际的试验说明一下。

主要分为matrix、center、fit三类

CENTER

Center the image in the view, but perform no scaling.

按图片的原来尺寸居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

CENTER_CROP

Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).

按原图比例放缩图片,使图片铺满屏幕,结果居中显示。

CENTER_INSIDE

Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

若原图尺寸小于view的尺寸,则不进行放缩,直接居中显示,否则按原图比例缩小图片至图片长宽都必须小于等于view的长宽。结果居中显示。

FIT_CENTER

Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.

按原图比例放缩图片,保证原图长宽中至少有一个值与view的长宽相等。结果居中显示。

FIT_END

Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. END aligns the result to the right and bottom edges of dst.

按原图比例放缩图片,保证原图长宽中至少有一个值与view的长宽相等。结果底部显示。

FIT_START

Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. START aligns the result to the left and top edges of dst.

按原图比例放缩图片,保证原图长宽中至少有一个值与view的长宽相等。结果顶部显示。

FIT_XY

Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.

不按比例缩放图片,目标是把图片塞满整个View。

如果不设置scaleType的话,则默认为FIT_CENTER