ImageView的全路径:android.widget.ImageView.显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。
1 XML属性
android:layout_width=""---控件宽
android:layout_height=""---控件高
android:scaleType=""---控件如何显示
参数:
center---按图片原来的尺寸居中显示,当图片的长(宽)超过view的长(宽),则截取图片居中部分显示
centerCrop---按比例扩大图片的尺寸居中显示,使得图片长(宽)等于或大于view的长(宽)
centerInside---将图片的内容完整居中显示,通过按比例缩小或原来的尺寸使得图片长(宽)小于或等于view的长(宽)
fitCenter---把图片按比例扩大/缩小到view的宽度,居中显示
fitEnd---把图片按比例扩大/缩小到view的宽度,显示在view的下半部分位置
fitStart---把图片按比例扩大/缩小到view 的宽度,显示在view的上半部分位置
fitXY---把图片不按比例扩大/缩小到view的大小显示
matrix---用矩阵来绘制
2 ImageView中常用方法说明
setAlpha(intalpha) 设置ImageView的透明度
setImageBitmap(Bitmap bm) 设置ImageView所显示的内容
为指定的Bitmap对象
setImageDrawable(Drawable drawable) 设置ImageView所显示的内容
为指定的Drawable对象
setImageResource(intresId) 设置ImageView所显示的内
容为指定id的资源
setImageURI(Uri uri) 设置ImageView所显示的内
容为指定Uri
setSelected(booleanselected) 设置ImageView的选中状态
3 ImageView例子
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="原图" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/head7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="AdjustViewBounds
ImageView大小自动调整,ImageView 没有红色背景" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:background="#ff0000"
android:maxHeight="170dp"
android:maxWidth="140dp"
android:src="@drawable/head7" />
<ImageView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:adjustViewBounds="true"
android:background="#ff0000"
android:maxHeight="170dp"
android:maxWidth="140dp"
android:src="@drawable/head7" />
<ImageView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:adjustViewBounds="true"
android:background="#ff0000"
android:maxHeight="170dp"
android:maxWidth="140dp"
android:src="@drawable/head7" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="cropToPadding属性" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#ff0000"
android:cropToPadding="true"
android:maxHeight="170dp"
android:maxWidth="140dp"
android:scrollY="-10dp"
android:src="@drawable/head7" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:cropToPadding="true"
android:maxHeight="170dp"
android:maxWidth="140dp"
android:scrollY="10dp"
android:src="@drawable/head7" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:cropToPadding="true"
android:maxHeight="170dp"
android:maxWidth="140dp"
android:scrollY="20dp"
android:src="@drawable/head7" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="140dp"
android:background="#ff0000"
android:scaleType="fitXY"
android:src="@drawable/head7" />
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="fitXY"
android:src="@drawable/head7" />
<ImageView
android:layout_width="170dp"
android:layout_height="180dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="fitXY"
android:src="@drawable/head7" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="fitStart" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="140dp"
android:background="#ff0000"
android:scaleType="fitStart"
android:src="@drawable/head7" />
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="fitStart"
android:src="@drawable/head7" />
<ImageView
android:layout_width="170dp"
android:layout_height="180dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="fitStart"
android:src="@drawable/head7" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="fitEnd" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="140dp"
android:background="#ff0000"
android:scaleType="fitEnd"
android:src="@drawable/head7" />
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="fitEnd"
android:src="@drawable/head7" />
<ImageView
android:layout_width="170dp"
android:layout_height="180dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="fitEnd"
android:src="@drawable/head7" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="fitCenter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="90dp"
android:background="#ff0000"
android:scaleType="fitCenter"
android:src="@drawable/head7" />
<ImageView
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="fitCenter"
android:src="@drawable/head7" />
<ImageView
android:layout_width="170dp"
android:layout_height="180dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="fitCenter"
android:src="@drawable/head7" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="140dp"
android:background="#ff0000"
android:scaleType="center"
android:src="@drawable/head7" />
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="center"
android:src="@drawable/head7" />
<ImageView
android:layout_width="170dp"
android:layout_height="180dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="center"
android:src="@drawable/head7" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="centerInside" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="90dp"
android:background="#ff0000"
android:scaleType="centerInside"
android:src="@drawable/head7" />
<ImageView
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="centerInside"
android:src="@drawable/head7" />
<ImageView
android:layout_width="170dp"
android:layout_height="180dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="centerInside"
android:src="@drawable/head7" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="90dp"
android:background="#ff0000"
android:scaleType="centerCrop"
android:src="@drawable/head7" />
<ImageView
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="centerCrop"
android:src="@drawable/head7" />
<ImageView
android:layout_width="170dp"
android:layout_height="180dp"
android:layout_marginLeft="10dp"
android:background="#ff0000"
android:scaleType="centerCrop"
android:src="@drawable/head7" />
</LinearLayout>
</LinearLayout>
</ScrollView>