【Android】ImageView.ScaleType說明

http://www.cnblogs.com/elvis_chow/archive/2011/12/10/2283314.html

http://myzh.me/blog/2011/12/android%E5%B7%A5%E4%BD%9C%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9B%BE%E7%89%87%E8%87%AA%E9%80%82%E5%BA%94imageview%E5%B1%9E%E6%80%A7androidscaletype/

以前習慣於從服務器下載圖片後,再寫一個工具類來縮減成指定的大小,然後放進指定控件.

其實不用那麼麻煩,ImageView控件中有一個android:scaleType屬性。
即ImageView.setScaleType(ImageView.ScaleType)
Sdk中介紹作用爲:Options for scaling the bounds of an image to the bounds of this view.
大體意思爲:一些縮放邊界來控制圖片視圖的界限範圍的選項

說白了,就是控制ImageView的邊界顯示方式.


CENTER
Center the image in the view, but perform no scaling.
按圖片的原來size居中顯示,當圖片長/寬超過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).
按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬)

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).
將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬

FIT_CENTER
Scale the image using CENTER.
把圖片按比例擴大/縮小到View的寬度,居中顯示

FIT_END
Scale the image using END.
把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置

FIT_START
Scale the image using START.
把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置

FIT_XY
Scale the image using FILL.
把圖片不按比例 擴大/縮小到View的大小顯示

MATRIX
Scale using the image matrix when drawing.
用矩陣來繪製

例如:
如果獲取到的圖片的長寬比例是固定的,假設1:1.
設計要去在480*800的分辨率上要顯示大小爲60*60
那對於的屬性就設置爲:

android:layout_width="40dip"
android:layout_height="40dip"
android:scaleType="fitXY"

這樣,只要你獲取到的圖片的比例是1:1,無論圖片多大,他都會按照60*60的大小來顯示。
(60px在480*800下爲40dip,(60 -0.5) / 1.5=…)

這樣就不需要單獨寫一個工具來縮放圖片了,而且在多分辨率適配的時候,適應能力也大大加強了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章