Android ImageView ScaleType 可视指南

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

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” 的方式,效果如下

image

参考文档

扩展

Building Ralph with SVG

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容