ImageView继承自View组件,它的主要功能是显示图片或者任何Drawable对象,下面是ImageView的类关系图:
由上图可以看出来,ImageView派生出ImageButton、ZoomButton等组件,因此ImageView支持的XML属性以及相关的方法也可以应用在ImageButton、ZoomButton上面。
下面是ImageView支持的XML属性以及相关方法的说明:
为了控制ImageView显示的图片,ImageView提供如下的方法:
ImageView派生出如下两个子类:
(1)ImageButton:图片按钮,这与Button按钮的区别在于:Button按钮可以显示文字,而ImageButton不能显示文字
(2)QuickContactBadge:显示关联到特定联系人的图片,QuickContactBadge继承了ImageView,因此它的本质也是图片,也可以通过android:src属性指定它显示的图片,QuickContactBadge特定的功能是:该图片可以关联到手机中指定的联系人,当用户单击图片时,系统将会打开相应联系人的联系方式界面
为了让QuickContactBadge与特定联系人相关联,可以调用如下方法进行关联:
ImageButton派生出一个子类ZoomButton,ZoomButton可以代表“放大”、“缩小”两个按钮,只要为ZoomButton的android:src属性分别指定btn_minus、btn_plus,即可实现“放大”、“缩小”的功能。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ImageButtonActivity" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blue" >
</ImageButton>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_selector" >
</ImageButton>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10sp"
android:orientation="horizontal" >
<ZoomButton
android:id="@+id/btn_zoom_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_minus" >
</ZoomButton>
<ZoomButton
android:id="@+id/btn_zoom_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_plus" >
</ZoomButton>
</LinearLayout>
<ZoomControls
android:id="@+id/zoomControls1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
</ZoomControls>
</LinearLayout>
显示的图片如下: