最近工作中的项目涉及到的新东西比较少,都是一些界面的实现,虽然不是太难的东西,但是也遇到了一些小坑,就此记录下来,防止以后再遇上,或者再遇上的时候可以迅速找到答案。
  今天要记录的是Android ImageView的ScaleType的问题,ScaleType属性用于可以让ImageView按一定规则缩放,以达到产品需求。ScaleType可以通过代码ImageView.setScaleType()来设置,也可以直接写在XML布局文件中。接下来详细解释几种效果。
  写在之前的话:ImageView的ScaleType其实还有个小坑在里头,就是当想使用ScaleType的时候,需要设置ImageView的src而不是background!!!否则ScaleType不生效。
  (1)CENTER
  Center the image in the view, but perform no scaling. 简单翻译就是将图片至于ImageView的中心,不压缩不拉伸,超过的部分会被裁剪。
  (2)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). 保持图片比例,放大图片,直到图片的宽高都大于等于ImageView的大小。简单总结就是以填满整个ImageView为目的。
  (3)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). 保持图片比例,保证图片的宽高都小于等于ImageView的大小。简单总结就是以原图能在ImageView完全显示为目的。但需要注意的是,如果图片本身比ImageView小,图片不会放大。
  (4)FIT_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.保持图片比例,放大或缩小,使得图片宽或高有一项等于ImageView的大小,另一项小于ImageView的大小,并在ImageView中心显示。FIT_CENTER和CENTER_INSIDE的区别在于FIT_CENTER会放大图片,而CENTER_INSIDE则不会。
  (5)FIT_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_CENTER的区别在于图片在ImageView底部显示。
  (6)FIT_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. END aligns the result to the right and bottom edges of dst.与FIT_CENTER类似,区别在于图片在ImageView的左上部显示。
  (6)FIT_XY
  Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.将图片完整显示在ImageView中,无视比例。
  (7)MATRIX
  Scale using the image matrix when drawing. 默认模式,不改变图片的大小,从ImageView左上角开始绘图,超过的部分裁剪。
  下面给大家看一张在网上搜到的网友总结的图,给大家直接感受。图片来源于网上,侵删。
  

更多相关文章

  1. Android(安卓)自定义Drawable 实现圆角圆形图片
  2. Android实现图片选择器功能
  3. android中Intent的一些用法和总结
  4. android如何实现文件按时间先后顺序排列显示
  5. 浅谈GridView
  6. 【Android(安卓)界面效果11】android 瀑布流效果(仿蘑菇街)
  7. Android(安卓)ViewPager实现循环轮播图
  8. Android简明开发教程二十一:访问Internet 绘制在线地图
  9. Android截屏方法总结

随机推荐

  1. Android(安卓)系统自带样式Android:theme
  2. EditText控件常用属性
  3. android inputtype属性
  4. Android(安卓)EditText各种属性及作用
  5. Android应用开发——系统自带样式Android
  6. Android开发_TextView跑马灯
  7. Android:Material Design(四) UI控件
  8. 命令行操作android工程
  9. Android(安卓)layout属性大全
  10. Android自带的theme