天天看點

Android中ImageView的ScaleType設定

近日發現網上好多關于ScaleType的說明都是錯誤的,是以根據官方文檔和實際的試驗說明一下。

主要分為matrix、center、fit三類

CENTER

Center the image in the view, but perform no scaling.

按圖檔的原來尺寸居中顯示,當圖檔長/寬超過View的長/寬,則截取圖檔的居中部分顯示

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).

若原圖尺寸小于view的尺寸,則不進行放縮,直接居中顯示,否則按原圖比例縮小圖檔至圖檔長寬都必須小于等于view的長寬。結果居中顯示。

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.

按原圖比例放縮圖檔,保證原圖長寬中至少有一個值與view的長寬相等。結果居中顯示。

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.

按原圖比例放縮圖檔,保證原圖長寬中至少有一個值與view的長寬相等。結果底部顯示。

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. START aligns the result to the left and top edges of dst.

按原圖比例放縮圖檔,保證原圖長寬中至少有一個值與view的長寬相等。結果頂部顯示。

FIT_XY

Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.

不按比例縮放圖檔,目标是把圖檔塞滿整個View。

如果不設定scaleType的話,則預設為FIT_CENTER