Android ImageView ScaleType 可视指南

如果你像我一样,你真的,非常,非常好看。 但你也可能有点健忘。 因此,当需要在ImageView中缩放图像时,你可能已经忘记不同的ScaleType显示在屏幕上具体是什么样子。 因此,您需要在接下来的10-15分钟内构建并使用每种比例类型重建您的应用,以查看它们的外观。 然后你不可避免地忘记了其中两个之间的区别,并重新开始整个过程。 正如孩子们所说,“我得到了”。以下并排放置了所有不同ScaleType 的屏幕截图。 所有的ScaleType 定义都是直接从Android官网文档复制过来的。 更为重要的是,对于勇于探索的你来说,这是一个有用的提示,他们在这篇文章的结尾。

3171886-8a8354767d644c64.jpg
image


Scale Types

完整的关于 Scale Types的介绍请看Android 官方文档


不同的 Scale Types 介绍

一:CENTER

将图像置于视图中心,但不缩放

二: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

对图片执行 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.


保持原图比例不变的情况下,计算出一个缩放比例,让缩放后的图片能完整显示在容器内(一般为view)并且至少保证一个边(长或宽)和容器(view) 完全重合,缩放后的图形置于视图中心。

五:FIT_END

对图片执行 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.


保持原图比例不变的情况下,计算出一个缩放比例,让缩放后的图片能完整显示在容器内(一般为view)并且至少保证一个边(长或宽)和容器(view) 完全重合,缩放后的图形置于容器右下方。

六:FIT_START

对图片执行 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.


保持原图比例不变的情况下,计算出一个缩放比例,让缩放后的图片能完整显示在容器内(一般为view)并且至少保证一个边(长或宽)和容器(view) 完全重合,缩放后的图形和容器保持左上对齐

七:FIT_XY

对图片执行 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.


对 X 和 Y 轴独立缩放,以准确的适应容器。很可能会改变图片的原始比例

八:MATRIX

Scale using the image matrix when drawing.

使用矩阵缩放图片


  1. ImageView.ScaleType.MATRIX lets you use a Matrix to scale the image. You can set the Matrix using ImageView.setImageMatrix(matrix). So by declaring the scaleType to MATRIX you are saying that you want to use an explicit Matrix to do that.
  2. You can use imageView.setScaleType(ImageView.ScaleType.MATRIX) whenever you want to customize the way the your image scales, rotates, etc. at your desire.
  1. FIT_END and FIT_START are default types of scale. So, if you use FIT_END for instance, your image will maintain the original aspect ratio and it will align the result of the right and bottom edges of your image view. So basically, the difference is that FIT_END and FIT_START are "presets" while with MATRIX you declare that you want to use your own matrix to scale.

  1. 允许使用矩阵(matrix)缩放图片,可以使用ImageView.setImageMatrix(matrix)设置matrix,所以申明 scaleTypeMATRIX 时,则表示你想通过设置一个确定的矩阵(MAXTRIX)来设置缩放。

  2. 您可以随时使用imageview.setscaleType(imageview.scaleType.matrix)的方式,根据需要自定义图像缩放、旋转等方式

  3. fit_endfit_start是默认的缩放类型。因此,例如,如果您使用fit_end,图像将保持原始比例缩放,缩放后的图片放置在视图的右边和底部。所以,区别在于fit-endfit-start是“预设”,而对于matrix,则声明要使用自己的matrix进行缩放。

  4. 可参考:what is the meaning of ImageView.ScaleType=“MATRIX”


九:Adjust View Bounds

While not technically an ImageView.ScaleType this will come in handy. If you notice with CENTER_INSIDE, FIT_CENTER, FIT_END and FIT_START the actual bounds of the ImageView are much larger than the scaled image. To set the bounds of the ImageView to the height of the image inside, use android:adjustViewBounds="true” in your XML. It looks like this:


Adjust View Bounds 并不是 ImageView.ScaleType 的属性,但它很有用,其实CENTER_INSIDE, FIT_CENTER, FIT_END and FIT_START 这些缩放方式 ImageView 的实际边界都是远远大于缩放后的图片的。要把 ImageView 的边界设置为它里面图片的高度,可以在xml 布局文件中使用 android:adjustViewBounds="true” 的方式,效果如下

3171886-fbbd9e1102bce21e.jpg
image

参考文档

扩展

Building Ralph with SVG

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值