2016-09-05 | learn

[翻译] 图解 Android ImageView ScaleType

Source: Android ImageView ScaleType: A Visual Guide
Author: Amanda Hill

如果你已经像我一样帅了,那么你很可能和我一样健忘~每次用 ImageViewScaleTypes 参数时总是拿不准各个type在屏幕上是什么样的,然后在接下来的十几分钟各种 build 试参数。过后你肯定还是记不住,无限次重复上述步骤。兄弟~你找到组织了!

下边的截图就是所有ScaleTypes对应的效果。图片下边是从 Android 官方文档复制粘贴过来的定义。结尾还有给读完这篇文章的勇士们准备的小 tips,很有用的 :)


scaletype

Scale Types

官方 Android documentation 里每个 ScaleType 的完整定义。

CENTER

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

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).

FIT_CENTER

Scale the image using Matrix.ScaleToFit.CENTER

Matrix.ScaleToFit.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.

FIT_END

Scale the image using Matrix.ScaleToFit.END

Matrix.ScaleToFit.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.

FIT_START

Scale the image using Matrix.ScaleToFit.START

Matrix.ScaleToFit.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.

FIT_XY

Scale the image using Matrix.ScaleToFit.FILL

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

MATRIX

Scale using the image matrix when drawing.


Adjust View Bounds

你或许已经注意到了,使用 CENTER_INSIDE, FIT_CENTER, FIT_END 还有 FIT_START 这几个属性时, ImageView 会比拉伸后的图片大。想要根据图来设置 ImageView 的 height 可以在 XML 里使用 android:adjustViewBounds="true"属性。

adjustviewbounds