ImageView屬性詳解

正文

  一、結構

    java.lang.Object

android.view.View

        android.widget.ImageView


    已知直接子類:

    ImageButton, QuickContactBadge


    已知間接子類:

    ZoomButton


  二、類概述

    顯示任意圖像,例如圖標。ImageView類可以加載各種來源的圖片(如資源或圖片庫),需要計算圖像的尺寸,比便它可以在其他佈局中使用,並提供例如縮放和着色(渲染)各種顯示選項。


  三、XML屬性


屬性名稱

描述

android:adjustViewBounds

是否保持寬高比。需要與maxWidth、MaxHeight一起使用,否則單獨使用沒有效果。

android:cropToPadding

是否截取指定區域用空白代替。單獨設置無效果,需要與scrollY一起使用,效果如下,實現代碼見代碼部分:

650) this.width=650;" width="153" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/2010-9-17_2.jpg" height="80" style="border:0px;" />


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的寬高

650) this.width=650;" border="0" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/scaleTypeFitXY.PNG" style="border:0px;" />


layout_

height

:30px


layout_

width

:120px

fitStart

2

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

650) this.width=650;" border="0" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/scaleTypeFitStart.PNG" style="border:0px;" />



fitCenter

3

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

650) this.width=650;" border="0" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/scaleTypeFitCenter.PNG" style="border:0px;" />



fitEnd

4

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

650) this.width=650;" border="0" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/scaleTypeFitEnd.PNG" style="border:0px;" />



center

5

按原圖大小顯示圖片,但圖片寬高大於View的寬高時,截圖圖片中間部分顯示650) this.width=650;" border="0" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/scaleTypeCenter.PNG" style="border:0px;" />



layout_

height

:60px


layout_

width

:80px


padding

:10px


centerCrop

6

按比例放大原圖直至等於某邊View的寬高顯示。650) this.width=650;" border="0" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/scaleTypeCenterCrop.PNG" style="border:0px;" />



centerInside

7

當原圖寬高或等於View的寬高時,按原圖大小居中顯示;反之將原圖縮放至View的寬高居中顯示。650) this.width=650;" border="0" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/scaleTypeCenterInside.PNG" style="border:0px;" />



android:src

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

android:tint

將圖片渲染成指定的顏色。見下圖:

650) this.width=650;" border="0" src="http://images.cnblogs.com/cnblogs_com/over140/2010/9/2010-9-17_1.jpg" style="border:0px;" />左邊爲原圖,右邊爲設置後的效果,見後面代碼。


  四、代碼  

   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"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

   4.2 android:cropToPadding

<ImageView android:background="@android:color/white" android:scrollY="-10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
    <ImageView android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
    <ImageView android:paddingTop="10px"  android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="true" android:src="@drawable/btn_mode_switch_bg"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
    <ImageView android:paddingTop="10px" android:background="@android:color/white" android:scrollY="10px" android:cropToPadding="false" android:src="@drawable/btn_mode_switch_bg"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>


參考:http://407827531.iteye.com/blog/1117199

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章