ScaleToFill、ScaleAspectFit、ScaleAspectFill 的区别

来源:互联网 发布:ubuntu16.04 ssd 优化 编辑:程序博客网 时间:2024/06/07 05:36

ScaleToFill、ScaleAspectFit、ScaleAspectFill

今天码代码又碰到这个,不理解透彻是真不行了,有点伤脑筋啊~


首先,百度谷歌了下,没啥好文章。草草看了几篇,基本都是差不多的内容。

然后,上stackoverflow,有人问过这三者之间有啥不同。第一个回答纯文字,如下:

UIViewContentModeScaleToFill 
Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.

UIViewContentModeScaleAspectFit 
Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.

UIViewContentModeScaleAspectFill 
Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

结果,纯解释看的我还是稀里糊涂,于是我继续往下看,居然有个配图的。如下:

2-1

有时候图片比文字直观太多了。今天我只是想解决前面三个,于是我仔细看了下,原始图片是212 * 80,放到一个100 * 100的视图View中,显然两者在尺寸上很不匹配。分析了下三者区别:

Scale To Fill 那个图片显然一张212 * 80 的图片要放到 100 * 100的视图中,要做的就是宽(212)要缩小到100,高(80)要放大到100,有点感觉像把图片在水平方向挤压似的。

Aspect Fit 这个图片显示真应了fit这个单词,通过放缩将(212,80)图片放入(100,100)的View中这个不用说,问题是与上面的放缩不同在于,它的宽高都是使用同一比例,宽212 * 0.4717 = 100,与上面不同,高80 * 0.4717 = 37.74,所以图片很真实,尽管缩小了0.4717比率

Aspect Fill 这个就应了Fill单词了,它和Fit不同,要把小的(也就是高80)放大起到填充的感觉,也就是80 * 1.25 = 100 那么我们的宽212,也要乘以1.25 = 265,最后得到一个(265,100)的图片,而我们的框框是(100,100),显然我们的视图显示图片余地有限,因此只能显示中间那一部分了。

0 0