天天看點

Android2.2 API 中文文檔系列(6) —— ImageView

一、結構

    java.lang.Object

        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

用矩陣來繪圖

fitXY

1

拉伸圖檔(不按比例)以填充View的寬高

layout_

height

:30px

width

:120px

fitStart

2

按比例拉伸圖檔,拉伸後圖檔的高度為View的高度,且顯示在View的左邊

fitCenter

3

按比例拉伸圖檔,拉伸後圖檔的高度為View的高度,且顯示在View的中間

fitEnd

4

按比例拉伸圖檔,拉伸後圖檔的高度為View的高度,且顯示在View的右邊

center

5

按原圖大小顯示圖檔,但圖檔寬高大于View的寬高時,截圖圖檔中間部分顯示 

:60px

:80px

padding

:10px

centerCrop

6

按比例放大原圖直至等于某邊View的寬高顯示。

centerInside

7

當原圖寬高或等于View的寬高時,按原圖大小居中顯示;反之将原圖縮放至View的寬高居中顯示。

android:src

設定View的drawable(如圖檔,也可以是顔色,但是需要指定View的大小)

android:tint

将圖檔渲染成指定的顔色。見下圖:

 左邊為原圖,右邊為設定後的效果,見後面代碼

四、代碼  

    4.1  android:tint

<ImageView android:background="@android:color/white" android:src="@drawable/btn_mode_switch_bg"

        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

    <ImageView android:layout_marginLeft="5dp" android:background="@android:color/white" android:tint="#ffff00" android:src="@drawable/btn_mode_switch_bg"

    4.2  android:cropToPadding

<ImageView android:background="@android:color/white" android:scrollY="-10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"

    <ImageView android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"

    <ImageView android:paddingTop="10px"  android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"

    <ImageView android:paddingTop="10px" android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="false" android:src="@drawable/btn_mode_switch_bg"

五、 系列

繼續閱讀