Android工作學習筆記之圖片自適應imageview屬性android:scaleType


安卓的適配一直是一件頭疼的事情.
特別是圖片.有的時候總是忽大忽小.
以前習慣於從服務器下載圖片后,再寫一個工具類來縮減成指定的大小,然后放進指定控件.

其實不用那么麻煩,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=…)

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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM