ImageView继承自View组件,主要功能是用于显示图片,实际上它不仅仅可以用来显示图片,任何Drawable对象都可以使用ImageView来显示。ImageView可以适用于任何布局中,并且Android为其提供了缩放和着色的一些操作。
属性:
scaleType缩放类型如下:
android:maxHeight和android:maxWidth表示图片的高和宽的最大值,同时加上android:adjustViewBounds="true"才生效,表四高或者宽两个满足一个最大时,另一个属性就会按比例自适应。
布局文件中如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:src="@drawable/ceshi1"
android:scaleType="fitCenter"
android:layout_width="200dp"
android:layout_height="200dp"/>
<ImageView
android:src="@drawable/ceshi2"
android:maxHeight="200dp"
android:maxWidth="200dp"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
效果如下: