通过本节课可以学习到的内容:
实例代码:
运行效果参见本课程示例App:安卓猴Demos
ImageView顾名思义,就是图片视图,用来显示静态图片。
<ImageView
android:id="@+id/imageview_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:onClick="ivClicked"
android:src="@mipmap/super_dog"
/>
<ImageView
android:id="@+id/imageview_super"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@android:color/black"
android:maxHeight="300dp"
android:maxWidth="300dp"
android:onClick="ivClicked"
android:src="@mipmap/super_dog"
/>效果图如下:

android:src,设置ImageView要显示的图片;android:adjustViewBounds,是否保持宽高比,需要和maxWidth、maxHeight一起使用,否则单独使用没有效果;android:maxWidth,设置View的最大宽度;android:maxHeight,设置View的最大高度;如果想设置图片固定大小,又想保持图片宽高比,需要这样设置:
android:adjustViewBounds设置为true;android:maxWidth和android:maxHeight要设置;android:layout_width和android:layout_height设置为wrap_content。android:scaleType是设置图片的填充方式,有以下用法:这一节课,我们主要学习了ImageView,不过它的功能有限,只能显示静态图片。在实际需求中,我们可能会用到更高级的用法,如显示圆形头像,或者动态的gif,又或者图片的缩放与缩略图等等,这些就需要我们自己取实现ImageView了,后面都会讲到。