imageView ScalType详解
最近一直在和图片打交道,每次使用scaleType 都是靠不停的试,看哪个图片展示最合适就使用哪个。被代码控制的感受不是很好啊,就花了点时间研究了下scaleType各种类型具体的含义。
废话不多说,先看看google官方的解释:
Google 官方解释
type | enum | 解释 |
---|---|---|
CENTER | Center the image in the view, but perform no scaling. | 图片居中放置,不做缩放,图片尺寸大于imageview,则从图片中间裁出同等尺寸图片放置,反之则直接居中放置。 |
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中宽高尺寸大的一边其余裁掉。图片120120, imageView 2030 缩小4倍后,裁掉 。放大:先等比放大图片,达到imageView中宽高中尺寸大的一边,后居中裁剪。例如:图片:2020,imageView 80180,会先放大9倍,后裁剪 |
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宽高中尺寸小的一边,其余裁掉。小于的话,则直接放置在中间 |
FIT_CENTER | Scale the image using Matrix.ScaleToFit.CENTER.Compute a sc FIT_CENTER ale 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宽高中尺寸较小的一边,imageView其余展示背景色。 |
FIT_END | Scale the image using 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. | 缩放类似与FIT_CENTER,图片靠END处展示 |
FIT_START | Scale the image using 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. | 缩放类似与FIT_CENTER,图片靠START处展示 |
FIT_XY | Scale the image using Matrix.ScaleToFit.FILL. Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src. | 宽高独立缩放,用于撑满整个imageView,图片宽高比可能发生变化 |
MATRIX | Scale using the image matrix when drawing. | 通过matrix控制图片缩放,在提供的已有方式中无法满足需求时,可使用 |
示例图
后面在多说一下matrix这个类型,基本在开发中没有用到过此类型,不过最强大的也是这个matrix,我也只是简单的尝试了下,可以通过matrix,来控制图片的展示方式:
//设置matrix
Matrix matrix = new Matrix();
float scale = (float) 0.2;
matrix.setScale(scale, scale);
mIvMatrix.setImageMatrix(matrix);
##总结
总的来说,在客户端展示上,用CENTER_CROP最优雅,图片不会被拉伸,也可以撑满整个imageView!