ImageView的ScaleType详解

ScaleType表示ImageView的缩放类型,决定了一张图片在ImageView控件内如何缩放和显示。

ScaleType的官方文档:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

ScaleType可以使用java代码或xml属性来设置。


ScaleType是一个枚举类型,定义如下。

public enum ScaleType {
    MATRIX      (0),

    FIT_XY      (1),

    FIT_START   (2),

    FIT_CENTER  (3),

    FIT_END     (4),

    CENTER      (5),

    CENTER_CROP (6),

    CENTER_INSIDE (7);

    ScaleType(int ni) {
        nativeInt = ni;
    }
    final int nativeInt;
}
以centerCrop为例,使用java代码设置,代码为imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)。

在xml布局文件中,可以使用如下属性。
android:scaleType="center"
android:scaleType="centerCrop"
android:scaleType="centerInside"
android:scaleType="fitStart"
android:scaleType="fitCenter"
android:scaleType="fitEnd"
android:scaleType="fitXY"
android:scaleType="matrix"


下面,结合示例详细介绍每一种类型。
首先准备两张示例图片,一张小图和一张大图,分别为adidas1.png(135*135)和adidas2.png(540*540)。
     
然后添加如下xml布局文件。将ImageView的宽高设置为150dp和100dp,以确保小图的尺寸小于ImageView的尺寸,大图的尺寸大于ImageView的尺寸。并将ImageView的背景色设置成灰色(#cccccc),方便查看效果。

<?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:background="#000000"
              android:gravity="center">
    <ImageView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:background="#cccccc"
        android:scaleType=""
        android:src="@drawable/adidas"/>
</LinearLayout>

进入ScaleType类型的介绍。

CENTER
Center the image in the view, but perform no scaling. 
把图片放置到ImageView的中心,不执行缩放。如果图片尺寸比ImageView小,会有空白区域;如果图片尺寸比ImageView大,图片的中间区域会被裁剪出来显示到ImageView中。
     

CENTER_CROP
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view.
把图片放置到ImageView的中心,执行缩放。将图片的宽度和高度按照相同比例缩放,直到图片的宽度大于或者等于ImageView的宽度,同时图片的高度大于或者等于ImageView的高度。
     

CENTER_INSIDE
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). 
把图片放置到ImageView的中心。如果图片的尺寸小于ImageView的尺寸,不缩放;否则,将图片的宽度和高度按照相同比例缩放,直到图片的宽度小于ImageView的宽度,同时图片的高度小于ImageView的高度。
     

FIT_CENTER
Scale the image using CENTER. 
把图片放置到ImageView的中心,然后按相同比例放大或缩小,直到图片宽度等于ImageView宽度,或者高度等于ImageView高度。
     

FIT_START
Scale the image using START. 
把图片放置到ImageView的左上角,然后按相同比例放大或缩小,直到图片宽度等于ImageView宽度,或者高度等于ImageView高度。
     

FIT_END
Scale the image using END. 
把图片放置到ImageView的右下角,然后按相同比例放大或缩小,直到图片宽度等于ImageView宽度,或者高度等于ImageView高度。
     

FIT_XY
Scale the image using FILL.
不保持图片的宽高比例,执行放大或缩小,直到图片宽度等于ImageView宽度,同时高度等于ImageView高度。
    

MATRIX
Scale using the image matrix when drawing. The image matrix can be set using setImageMatrix(Matrix).
不缩放图片,用图片的矩阵从左上角开始来绘制。

     


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值