ImageView ScaleType

来源:互联网 发布:精密减速机 知乎 编辑:程序博客网 时间:2024/05/21 10:35

之前一直对ImageView的多个Scaletype缩放类型的几个设置参数一知半解,看网上的各种解释也是当时明白事后就忘记了,最后通过查看源码注释和写demo的方式基本弄明白了其中的差别。

  1. fitxy: Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.
    独立的缩放x y 方向的长度,所以源图片可以准确的匹配目标imageview.这种设置可以会改变源图片的长宽比例。(比例会改变)
  2. fitCenter: 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.
    计算源图的长宽(纵横)比例,确保原图将会完整的在目标imageview里,至少将会有一个方向的长度是准确匹配的。结果就是源图会在目标view中间位置。
    这个应该是一个默认设置。举个例子bitmap 600 400 imgage 600 200 ;源图比例为3:2 ,缩放之后也要保持这个比例,要完整展示,则y要变为50%,x也要变为50%.会将就短的。结果就是y方向高度会正好充满imageView,而x方向上只会占据一半的空间,并居中显示。

3.Center: Center the image in the view, but perform no scaling. From XML, use this syntax: android:scaleType=”center”.
将图片居中放置,不做任何缩放。
从例子中看到相当于把照片居中放到相框中,不做移动和缩放,超出的就超出了,空出的就空出了

4. centerCrop: 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: android:scaleType=”centerCrop”.
保持图片的寬高比,一致地缩放图片,所以两个维度的长度(宽和高)会等于或者大于view的相应的边长(减去padding).图片会居中放置到view中。
如bitmap 600 400 imageview 800 600 ,原图比例为3:2 ,相框要比图片大,开始等比放大直到两个边都大于等于view的边长,当width 放大到800时,height=400 *(8/6)=533 小于view的hight600,还要继续放大,直到hight正好为600.因此hight纵向上会准确,横向上会有上下的截取。

5 centerInside: 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: android:scaleType=”centerInside”.
保持图片的寬高比,一致地缩放图片,所以两个维度的长度(宽和高)会等于或者小于view的相应的边长(减去padding).图片会居中放置到view中。这个与centerCrop的区别是图片的维度是不大于view的长度,centerCrop是不小于 。

6. centerInside效果类似于fitcenter.先满足相对较长的边满足尺寸,较短的边会留有空隙了。在bitmap尺寸大于imageview时,两者都会缩小短边,因此效果非常类似。
两者的区别在于:在imageview的尺寸大于bitmap时,fitcenter会放大,使较短的边能够匹配。而centerinside却不会,因为已经小于view的边了。

原创粉丝点击