如果你像我一樣湃窍,你真的闻蛀,非常,非常好看您市。 但你也可能有點(diǎn)健忘觉痛。 因此,當(dāng)需要在ImageView
中縮放圖像時(shí)茵休,你可能已經(jīng)忘記不同的ScaleType
顯示在屏幕上具體是什么樣子薪棒。 因此手蝎,您需要在接下來(lái)的10-15分鐘內(nèi)構(gòu)建并使用每種比例類(lèi)型重建您的應(yīng)用,以查看它們的外觀盗尸。 然后你不可避免地忘記了其中兩個(gè)之間的區(qū)別柑船,并重新開(kāi)始整個(gè)過(guò)程帽撑。 正如孩子們所說(shuō)泼各,“我得到了”。以下并排放置了所有不同ScaleType
的屏幕截圖亏拉。 所有的ScaleType
定義都是直接從Android
官網(wǎng)文檔復(fù)制過(guò)來(lái)的扣蜻。 更為重要的是,對(duì)于勇于探索的你來(lái)說(shuō)及塘,這是一個(gè)有用的提示莽使,他們?cè)谶@篇文章的結(jié)尾。
Scale Types
完整的關(guān)于 Scale Types的介紹請(qǐng)看Android 官方文檔
不同的 Scale Types 介紹
一:CENTER
將圖像置于視圖中心笙僚,但不縮放
二: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).
均勻縮放圖像(保持圖像的縱橫比)芳肌,使圖像的尺寸(寬度和高度)等于或大于視圖的相應(yīng)尺寸(減去填充)。
三: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).
均勻縮放圖像(保持圖像的縱橫比)肋层,使圖像的尺寸(寬度和高度)等于或小于視圖的相應(yīng)尺寸(減去填充)
四:FIT_CENTER
對(duì)圖片執(zhí)行 Matrix.ScaleToFit.CENTER
縮放
Matrix.ScaleToFit.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.
保持原圖比例不變的情況下亿笤,計(jì)算出一個(gè)縮放比例,讓縮放后的圖片能完整顯示在容器內(nèi)(一般為view
)并且至少保證一個(gè)邊(長(zhǎng)或?qū)挘┖腿萜鳎?code>view) 完全重合栋猖,縮放后的圖形置于視圖中心净薛。
五:FIT_END
對(duì)圖片執(zhí)行 Matrix.ScaleToFit.END
縮放
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.
保持原圖比例不變的情況下,計(jì)算出一個(gè)縮放比例蒲拉,讓縮放后的圖片能完整顯示在容器內(nèi)(一般為view
)并且至少保證一個(gè)邊(長(zhǎng)或?qū)挘┖腿萜鳎?code>view) 完全重合肃拜,縮放后的圖形置于容器右下方。
六:FIT_START
對(duì)圖片執(zhí)行 Matrix.ScaleToFit.START
縮放
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.
保持原圖比例不變的情況下雌团,計(jì)算出一個(gè)縮放比例燃领,讓縮放后的圖片能完整顯示在容器內(nèi)(一般為view
)并且至少保證一個(gè)邊(長(zhǎng)或?qū)挘┖腿萜鳎?code>view) 完全重合,縮放后的圖形和容器保持左上對(duì)齊
七:FIT_XY
對(duì)圖片執(zhí)行 Matrix.ScaleToFit.FILL
縮放
Matrix.ScaleToFit.FILL: Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.
對(duì) X 和 Y 軸獨(dú)立縮放锦援,以準(zhǔn)確的適應(yīng)容器猛蔽。很可能會(huì)改變圖片的原始比例
八:MATRIX
Scale using the image matrix when drawing.
使用矩陣縮放圖片
- ImageView.ScaleType.MATRIX lets you use a Matrix to scale the image. You can set the Matrix using ImageView.setImageMatrix(matrix). So by declaring the scaleType to MATRIX you are saying that you want to use an explicit Matrix to do that.
- You can use imageView.setScaleType(ImageView.ScaleType.MATRIX) whenever you want to customize the way the your image scales, rotates, etc. at your desire.
- FIT_END and FIT_START are default types of scale. So, if you use FIT_END for instance, your image will maintain the original aspect ratio and it will align the result of the right and bottom edges of your image view. So basically, the difference is that FIT_END and FIT_START are "presets" while with MATRIX you declare that you want to use your own matrix to scale.
允許使用矩陣(
matrix
)縮放圖片,可以使用ImageView.setImageMatrix(matrix)
設(shè)置matrix
,所以申明scaleType
為MATRIX
時(shí)雨涛,則表示你想通過(guò)設(shè)置一個(gè)確定的矩陣(MAXTRIX)來(lái)設(shè)置縮放枢舶。您可以隨時(shí)使用
imageview.setscaleType(imageview.scaleType.matrix)
的方式,根據(jù)需要自定義圖像縮放替久、旋轉(zhuǎn)等方式fit_end
和fit_start
是默認(rèn)的縮放類(lèi)型凉泄。因此,例如蚯根,如果您使用fit_end
雕沉,圖像將保持原始比例縮放,縮放后的圖片放置在視圖的右邊和底部叛甫。所以避咆,區(qū)別在于fit-end
和fit-start
是“預(yù)設(shè)”,而對(duì)于matrix
滩愁,則聲明要使用自己的matrix
進(jìn)行縮放。
九:Adjust View Bounds
While not technically an ImageView.ScaleType this will come in handy. If you notice with CENTER_INSIDE, FIT_CENTER, FIT_END and FIT_START the actual bounds of the ImageView are much larger than the scaled image. To set the bounds of the ImageView to the height of the image inside, use android:adjustViewBounds="true” in your XML. It looks like this:
Adjust View Bounds
并不是 ImageView.ScaleType
的屬性,但它很有用括堤,其實(shí)CENTER_INSIDE, FIT_CENTER, FIT_END and FIT_START
這些縮放方式 ImageView 的實(shí)際邊界都是遠(yuǎn)遠(yuǎn)大于縮放后的圖片的。要把 ImageView 的邊界設(shè)置為它里面圖片的高度绍移,可以在xml 布局文件中使用 android:adjustViewBounds="true”
的方式悄窃,效果如下