Android ImageView圖片自適應
網絡上下載下來的圖片自適應:android:adjustViewBounds="true"(其詳細解釋在下面)
<ImageView
android:id="@+id/dynamic_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginTop="5dip"
android:adjustViewBounds="true"
android:background="@drawable/imageview_background" />
另外,android:background="@drawable/imageview_background"是給圖片加了一個邊框,其中
imageview_background.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/white"/> <stroke android:width="2.0dip" android:color="#99D9D9D9" /> <corners android:radius="2.0dip" /> <padding android:left="5.0dip" android:top="5.0dip" android:right="5.0dip" android:bottom="5.0dip" /> </shape>
ImageView屬性說明:
1、類概述
顯示任意圖像,例如圖標。ImageView類可以加載各種來源的圖片(如資源或圖片庫),需要計算圖像的尺寸,比便它可以在其他布局中使用,并提供例如縮放和著色(渲染)各種顯示選項。
2、XML屬性
屬性名稱 </td> |
描述 </td> </tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
android:adjustViewBounds </td> |
是否保持寬高比。需要與maxWidth、MaxHeight一起使用,否則單獨使用沒有效果。 </td> </tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
android:cropToPadding </td> |
是否截取指定區域用空白代替。單獨設置無效果,需要與scrollY一起使用,效果如下,實現代碼見代碼部分: </td> </tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
android:maxHeight </td> |
設置View的最大高度,單獨使用無效,需要與setAdjustViewBounds一起使用。如果想設置圖片固定大小,又想保持圖片寬高比,需要如下設置: 1) 設置setAdjustViewBounds為true; 2) 設置maxWidth、MaxHeight; 3) 設置設置layout_width和layout_height為wrap_content。 </td> </tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
android:maxWidth </td> |
設置View的最大寬度。同上。 </td> </tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
android:scaleType </td> |
設置圖片的填充方式。
| |