標籤:
注意
請查看本文後期更新完整版:
http://www.cnblogs.com/over140/archive/2011/06/08/2075054.html
來源: 農民伯伯: http://www.cnblogs.com/over140/
本文
一、結構
java.lang.Object
android.view.View
android.widget.ImageView
已知直接子類:
ImageButton, QuickContactBadge
已知間接子類:
ZoomButton
二、類概述
顯示任意映像,例標。ImageView類可以載入各種來源的圖片(如資源或圖片庫),需要計算映像的尺寸,比便它可以在其他布局中使用,並提供例如縮放和著色(渲染)各種顯示選項。
三、XML屬性
屬性名稱 |
描述 |
android:adjustViewBounds |
是否保持寬高比。需要與maxWidth、MaxHeight一起使用,否則單獨使用沒有效果。 |
android:cropToPadding |
是否截取指定地區用空白代替。單獨設定無效果,需要與scrollY一起使用,效果如下,實現代碼見代碼部分: |
android:maxHeight |
設定View的最大高度,單獨使用無效,需要與setAdjustViewBounds一起使用。如果想設定圖片固定大小,又想保持圖片寬高比,需要如下設定: 1) 設定setAdjustViewBounds為true; 2) 設定maxWidth、MaxHeight; 3) 設定設定layout_width和layout_height為wrap_content。 |
android:maxWidth |
設定View的最大寬度。同上。 |
android:scaleType |
設定圖片的填充方式。
matrix |
0 |
用矩陣來繪圖 |
|
fitXY |
1 |
展開圖片(不按比例)以填充View的寬高 |
layout_ height :30px layout_
width :120px |
fitStart |
2 |
按比例展開圖片,展開後圖片的高度為View的高度,且顯示在View的左邊 |
fitCenter |
3 |
按比例展開圖片,展開後圖片的高度為View的高度,且顯示在View的中間 |
fitEnd |
4 |
按比例展開圖片,展開後圖片的高度為View的高度,且顯示在View的右邊 |
center |
5 |
按原圖大小顯示圖片,但圖片寬高大於View的寬高時,圖片中間部分顯示 |
layout_ height :60px layout_
width :80px padding
:10px |
centerCrop |
6 |
按比例放大原圖直至等於某邊View的寬高顯示。 |
centerInside |
7 |
當原圖寬高或等於View的寬高時,按原圖大小置中顯示;反之將原圖縮放至View的寬高置中顯示。 |
|
android:src |
設定View的drawable(片,也可以是顏色,但是需要指定View的大小) |
android:tint |
將圖片渲染成指定的顏色。見: 左邊為原圖,右邊為設定後的效果,見後面代碼。 |
四、代碼
4.1 android:tint
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:src="@drawable/btn_mode_switch_bg" ></ImageView><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:src="@drawable/btn_mode_switch_bg" android:tint="#ffff00" ></ImageView>
4.2 android:cropToPadding
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:cropToPadding="true" android:scrollY="-10px" android:src="@drawable/btn_mode_switch_bg" ></ImageView><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:cropToPadding="true" android:scrollY="10px" android:src="@drawable/btn_mode_switch_bg" ></ImageView><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:cropToPadding="true" android:paddingTop="10px" android:scrollY="10px" android:src="@drawable/btn_mode_switch_bg" ></ImageView><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:cropToPadding="false" android:paddingTop="10px" android:scrollY="10px" android:src="@drawable/btn_mode_switch_bg" ></ImageView>
五、 系列
Android2.2 API 中文文檔系列(1) —— TextView
Android2.2 API 中文文檔系列(2) —— EditText
Android2.2 API 中文文檔系列(3) —— AccessibilityService
Android2.2 API 中文文檔系列(4) —— Manifest
Android2.2 API 中文文檔系列(5) —— View
結束
本文android:tint屬性比較有意思,而android:adjustViewBounds與android:cropToPadding是痛點,都花了超過一個小時才試出效果。
Android2.2 API —— ImageView