为了账号安全,请及时绑定邮箱和手机立即绑定

Android ImageView的ScaleType属性

标签:
Android

ScaleType属性

常量含义
fitXY横向、纵向独立缩放,以适应该ImageView
fitCenter保持纵横比缩放图片,缩放完成后将图片放在ImageView的中央
fitStart保持纵横比缩放图片,并且将图片放在ImageView的左上角
fitEnd保持纵横比缩放图片,缩放完成后将图片放在ImageView的右下角
center把图片放在ImageView的中央,但是不进行任何缩放
centerCrop保持纵横比缩放图片,以使图片能完全覆盖ImageView
centerInside保持纵横比缩放图片,以使得ImageView能完全显示该图片
matrix使用matrix方式进行缩放

截图1:没啥属性,就wrap_content后者设置高宽等,代码就略过

webp

image.png

截图2:ScaleType=fitXY,fitCenter,fitStart,fitEnd

webp

image.png

       <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="@color/blue"
            android:text="下面ImageView固定设置:200dp*100dp,并开始加上ScaleType属性了"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1、scaleTyple=fitXY,显然是缩放到XY高度,被拉伸了"/>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="fitXY"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2、scaleTyple=fitCenter,按比例适配缩放后居中,也是默认形式哦"/>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="fitCenter"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3、scaleTyple=fitStart,按比例适配缩放后左上"/>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="fitStart"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4、scaleTyple=fitEnd,按比例适配缩放后右下"/>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="fitEnd"/>

截图3:ScaleType=center,centerCrop,centerInside

webp

image.png

       <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5、scaleTyple=center,显然按照默认图大小,画在中间了"/>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="center"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用50*50大小试试看,scaleTyple=center,好吧,就一个笑脸了,原图不变,尺寸小就小咯"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="center"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6、scaleTyple=centerCrop,这个厉害了,为了等比例适配后还改变了原先控件大小!!"/>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="centerCrop"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7、scaleTyple=centerInside,感觉和center一样啊"/>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="centerInside"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用50*50大小试试看,scaleTyple=centerInside,按比例缩小后显示在内部"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="centerInside"/>

截图4:ScaleType=matrix

webp

image.png

       <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8、scaleTyple=matrix,默认尺寸左上排列"/>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/namei"
            android:background="@color/pink"
            android:scaleType="matrix"/>

ImageView加载图片,代码部分

//4种方式设置图片iv_nm.setImageResource(R.mipmap.namei);
iv_nm.setImageDrawable(getResources().getDrawable(R.mipmap.namei));
iv_nm.setImageBitmap(BitmapFactory.decodeFile("file path"));
iv_nm.setImageURI(Uri.parse("file path"));



作者:Kandy_JS
链接:https://www.jianshu.com/p/cebfaa7f81ac


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消