ImageView類可以加載各種來源的圖片(如資源或圖片庫),需要計算圖像的尺寸,比便它可以在其他佈局中使用,並提供例如縮放和著色(渲染)各種顯示選項。
<1>
屬性
屬性名 對應的方法 描述
android:adjustViewBounds setAdjustViewBounds(boolean)
設置該屬性為真可以在ImageView 調整邊界時保持圖片的縱橫比例。(需要與maxWidth、MaxHeight一起使用,否則單獨使用沒有效果。)
android:baseline setBaseline(int)
視圖內基線的偏移量
android:baselineAlignBottom setBaselineAlignBottom(boolean)
如果為true,圖像視圖將基線與父控件底部邊緣對齊。
android:cropToPadding 如果為真,會剪切圖片以適應內邊距的大小。
android:maxHeight setMaxHeight(int) 為視圖提供最大高度的可選參數
android:maxWidth setMaxWidth(int)
為視圖提供最大寬度的可選參數。
android:scaleType setScaleType(ImageView.ScaleType) 控制為瞭使圖片適合ImageView 的大小,應該如何變更圖片大小或移動圖片
android:src setImageResource(int)
設置可繪制對象作為ImageView 顯示的內容
android:tint setColorFilter(int,PorterDuff.Mode) 為圖片設置著色顏色
<2>
方法:
public void setMaxHeight (int maxHeight)
用於設置該視圖支持的最大高度的可選參數。隻有setAdjustViewBounds(boolean) 為真時有效。要設置圖像最大尺寸為100×100,並保持原始比率,做法如下:
1) 設置adjustViewBounds 為真;
2) 設置maxWidth 和maxHeight 為100;
3) 設置寬、高的佈局參數為WRAP_CONTENT。
註意,如果原始圖像較小,即使設置瞭該參數,圖像仍然要比100×100 小。如果要設置圖片為 固定大小,需要在佈局參數中指定大小,並使用setScaleType(ImageView.ScaleType) 函數來檢測,如何 將其調整到適當的大小。
參數
maxHeight 該視圖的最大高度。
相關XML 屬性
android:maxHeight
public void setMaxWidth (int maxWidth)
用於設置該視圖支持的最大寬度的可選參數。隻有setAdjustViewBounds(boolean) 為真時有效。要設置圖像最大尺寸為100×100,並保持原始比率,做法如下:
4) 設置adjustViewBounds 為真;
5) 設置maxWidth 和maxHeight 為100;
6) 設置寬、高的佈局參數為WRAP_CONTENT。
註意,如果原始圖像較小,即使設置瞭該參數,圖像仍然要比100×100 小。如果要設置圖片為 固定大小,需要在佈局參數中指定大小,並使用setScaleType(ImageView.ScaleType) 函數來檢測,如何 將其調整到適當的大小。
參數
maxWidth 該視圖的最大寬度。
相關XML 屬性
android:maxWidth
public void setScaleType (ImageView.ScaleType scaleType)
控制圖像應該如何縮放和移動,以使圖像與ImageView 一致。
參數
scaleType 需要的縮放方式。
相關XML 屬性
android:scaleType
public void setSelected (boolean selected)
改變視圖的選中狀態。視圖有選中和未選中兩個狀態。註意,選擇狀態不同於焦點。 典型的選中的視圖是象ListView 和GridView 這樣的AdapterView 中顯示的 內容;選中的內容會顯示為高亮。
參數
selected 為真,將視圖設為選中狀態;否則為假。
public void setImageURI (Uri uri)
設置指定的URI 為該ImageView 顯示的內容。
該操作讀取位圖,並在UI 線程中解碼,因此可能導致反應遲緩。 如果反應遲緩,可以考慮用setImageDrawable(Drawable)、setImageBitmap(Bitmap) 或者BitmapFactory 代替。
參數
uri 圖像的URI。
public void setImageBitmap (Bitmap bm)
設置位圖作為該ImageView 的內容。
參數
bm 設置的位圖。
public void setImageDrawable (Drawable drawable)
設置可繪制對象為該ImageView 顯示的內容。
參數
drawable 設置的可繪制對象。
public void setAdjustViewBounds (boolean adjustViewBounds)
當你需要在ImageView 調整邊框時保持可繪制對象的比例時,將該值設為真。
參數
adjustViewBounds 是否調整邊框,以保持可繪制對象的原始比例。
相關XML 屬性
android:adjustViewBounds
public void setAlpha (int alpha)
設置透明度
<3>
ScaleType屬性:
public static final ImageView.ScaleType CENTER
在視圖中使圖像居中,不執行縮放。 在XML 中可以使用的語法:android:scaleType="center"。
public static final ImageView.ScaleType CENTER_CROP
均衡的縮放圖像(保持圖像原始比例),使圖片的兩個坐標(寬、高)都大於等於 相應的視圖坐標(負的內邊距)。圖像則位於視圖的中央。 在XML 中可以使用的語法:android:scaleType="centerCrop"。
public static final ImageView.ScaleType CENTER_INSIDE
均衡的縮放圖像(保持圖像原始比例),使圖片的兩個坐標(寬、高)都小於等於 相應的視圖坐標(負的內邊距)。圖像則位於視圖的中央。 在XML 中可以使用的語法:android:scaleType="centerInside"。
public static final ImageView.ScaleType FIT_CENTER
使用CENTER 方式縮放圖像。 在XML 中可以使用的語法:android:scaleType="fitCenter"。
public static final ImageView.ScaleType FIT_END
使用END 方式縮放圖像。 在XML 中可以使用的語法:android:scaleType="fitEnd"。
public static final ImageView.ScaleType FIT_START
使用START 方式縮放圖像。 在XML 中可以使用的語法:android:scaleType="fitStart"。
public static final ImageView.ScaleType FIT_XY
使用FILL 方式縮放圖像。 在XML 中可以使用的語法:android:scaleType="fitXY"。
public static final ImageView.ScaleType MATRIX
繪制時,使用圖像矩陣方式縮放。圖像矩陣可以通過setImageMatrix(Matrix) 設置。在XML 中可以使用的語法:android:scaleType="matrix"。
案例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="scaleType:fitXY"/>
<ImageView
android:id="@+id/image1"
android:adjustViewBounds="true"
android:layout_width= "300dp"
android:layout_height="100dp"
android:src= "@drawable/macos01"
android:scaleType="fitXY"
android:background="#F00"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="scaleType:center"/>
<ImageView
android:id="@+id/image2"
android:adjustViewBounds="true"
android:layout_width= "300dp"
android:layout_height="100dp"
android:src= "@drawable/macos04"
android:scaleType="center"
android:background="#F00"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="scaleType:fitEnd"/>
<ImageView
android:id="@+id/image3"
android:adjustViewBounds="true"
android:layout_width= "300dp"
android:layout_height="100dp"
android:src= "@drawable/macos01"
android:scaleType="fitEnd"
android:background="#F00"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="scaleType:fitCenter"/>
<ImageView
android:id="@+id/image4"
android:adjustViewBounds="true"
android:layout_width= "300dp"
android:layout_height="80dp"
android:src= "@drawable/macos04"
android:scaleType="fitCenter"
android:background="#F00"/>
</LinearLayout>
摘自 落日小屋