ImageView ScaleType

113 阅读4分钟

ImageView的ScaleType一共有八种类型,分别是:

image.png

由于不同类型的ScaleType最终展示的ImageView不一样,而且我们的ImageView使用频率非常高,因此,了解不同类型的ScaleType的作用是十分必要的。

已知图片宽高是800*500

image.png

1 CENTER

image.png 在不缩放的前提下,将Image的中间显示在ImageView的中间

当Image尺寸比ImageView尺寸大 那么只显示Image中间与ImageView尺寸相等的部分

当Image尺寸与ImageView尺寸相等 完整显示

当Image尺寸比ImageView尺寸小 那么Image完整地展示在ImageView中间

image.png

2 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). The image is then centered in the view.
 * From XML, use this syntax: <code>android:scaleType="centerCrop"</code>.
 */

Image Width Height等比例缩放至至少在一个方向上Image与ImageView尺寸一样(Image短边缩放至与ImageView对应边相等),之后将缩放后的Image的中间显示在ImageView的中间。

Image尺寸比ImageView尺寸大 那么Image短边缩小至与ImageView对应边相等,Image长边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView中间

当Image尺寸与ImageView尺寸相等 完整显示

当Image尺寸比ImageView尺寸小 那么Image短边放大至与ImageView对应边相等,Image长边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView中间

image.png

3 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). The image is then centered in the view.
 * From XML, use this syntax: <code>android:scaleType="centerInside"</code>.
 */

Image尺寸比ImageView尺寸大 那么Image长边缩小至与ImageView对应边相等,Image短边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView中间。

当Image尺寸与ImageView尺寸相等 完整显示

当Image尺寸比ImageView尺寸小 Image不作任何处理,直接显示在ImageView中间。

4 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.
 */

Image尺寸比ImageView尺寸大 那么Image长边缩小至与ImageView对应边相等,Image短边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView中间

当Image尺寸与ImageView尺寸相等 完整显示

当Image尺寸比ImageView尺寸小 那么Image长边放大至与ImageView对应边相等,Image短边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView中间

5 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.
 */

Image尺寸比ImageView尺寸大 那么Image长边缩小至与ImageView对应边相等,Image短边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView右边或下边

当Image尺寸与ImageView尺寸相等 完整显示

当Image尺寸比ImageView尺寸小 那么Image长边放大至与ImageView对应边相等,Image短边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView右边或下边

6 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.
 */

Image尺寸比ImageView尺寸大 那么Image长边缩小至与ImageView对应边相等,Image短边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView左边或上边

当Image尺寸与ImageView尺寸相等 完整显示

当Image尺寸比ImageView尺寸小 那么Image长边放大至与ImageView对应边相等,Image短边根据相应的缩放系数进行缩放,之后将Image中间显示在ImageView左边或上边

7 FIT_XY

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

完整地将Image显示在ImageView里面,Image X,Y方向上分别缩放至与ImageView对应边相等,Image的宽和高缩放系数可以不一致。

Image尺寸比ImageView尺寸大 那么Image的宽和高分别缩放至ImageView对应边相等,之后将Image完整地显示在ImageView里面。

当Image尺寸与ImageView尺寸相等 完整显示

当Image尺寸比ImageView尺寸小 那么Image的宽和高分别缩放至ImageView对应边相等,之后将Image完整地显示在ImageView里面。

8 MATRIX

/**
 * Scale using the image matrix when drawing. The image matrix can be set using
 * {@link ImageView#setImageMatrix(Matrix)}. From XML, use this syntax:
 * <code>android:scaleType="matrix"</code>.
 */

不常用。