ImageView的scaleType的属性有好几种,分别是
matrix(默认):
不改变原图的大小,从ImageView的左上角开始绘制原图,原图超过ImageView的部分作裁剪处理。
center:
保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理。
centerCrop:
以填满整个ImageView为目的,等比缩放,居中,超出裁剪,不会留空白
centerInside:
以完全显示为目的,等比缩放,居中,可能有空白
fitCenter
把原图按比例扩大或缩小到ImageView的ImageView的高度,居中显示
fitEnd
把原图按比例扩大(缩小)到ImageView的高度,显示在ImageView的下部分位置
fitStart
把原图按比例扩大(缩小)到ImageView的高度,显示在ImageView的上部分位置
fitXY
把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageView.
示例一:fitCenter/fitStart/fitEnd/fitXY
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv11"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv22"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="fitStart"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv33"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="fitEnd"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv44"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</HorizontalScrollView>
示例二:fitCenter/fitStart/fitEnd/fitXY
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="fitStart"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="fitEnd"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</ScrollView>
示例三:center/centerInside/centerCrop
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv1"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="center"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv2"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv3"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</ScrollView>
示例四:center/centerInside/centerCrop
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="center"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv2"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv3"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</ScrollView>
示例五:matrix
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="matrix"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv2"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="matrix"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="#ff0000"
android:scaleType="matrix"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</ScrollView>