图片自适应imageview属性android:scaleType

本文介绍了如何使用Android的ImageView控件中的scaleType属性来解决图片在不同分辨率屏幕上的适配问题,避免图片显示时出现拉伸或失真,并提供了一些具体的scaleType属性设置示例。

安卓的适配一直是一件头疼的事情.
特别是图片.有的时候总是忽大忽小.
以前习惯于从服务器下载图片后,再写一个工具类来缩减成指定的大小,然后放进指定控件.

其实不用那么麻烦,ImageView控件中有一个android:scaleType属性。
即ImageView.setScaleType(ImageView.ScaleType)
Sdk中介绍作用为:Options for scaling the bounds of an image to the bounds of this view.
大体意思为:一些缩放边界来控制图片视图的界限范围的选项

说白了,就是控制ImageView的边界显示方式.

CENTER
Center the image in the view, but perform no scaling.
按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示.

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).
按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)

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).
将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽

FIT_CENTER
Scale the image using CENTER.
把图片按比例扩大/缩小到View的宽度,居中显示

FIT_END
Scale the image using END.
把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

FIT_START
Scale the image using START.
把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

FIT_XY
Scale the image using FILL.
把图片不按比例 扩大/缩小到View的大小显示

MATRIX
Scale using the image matrix when drawing.
用矩阵来绘制

 

例如:
如果获取到的图片的长宽比例是固定的,假设1:1.
设计要去在480*800的分辨率上要显示大小为60*60
那对于的属性就设置为:

android:layout_width="40dip"
android:layout_height="40dip"
android:scaleType="fitXY"

这样,只要你获取到的图片的比例是1:1,无论图片多大,他都会按照60*60的大小来显示。
(60px在480*800下为40dip,(60 -0.5) / 1.5=…)

这样就不需要单独写一个工具来缩放图片了,而且在多分辨率适配的时候,适应能力也大大加强了。

转载地址:http://blog.youkuaiyun.com/q_zhe/article/details/7822984

<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/e" tools:context=".fragment.UserFragment"> <!-- 修正为正确的Fragment类名 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 顶部用户信息区域 --> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_welcome"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:padding="48dp"> <!-- 用户头像(优化尺寸适配) --> <com.google.android.material.imageview.ShapeableImageView android:id="@+id/iv_avatar" android:layout_width="80dp" android:layout_height="80dp" android:padding="2dp" android:src="@drawable/user_icon" android:scaleType="centerCrop" app:shapeAppearanceOverlay="@style/circleImageStyle" app:strokeColor="#FFFFFF" app:strokeWidth="2dp" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="欢迎您,亲爱的花主" android:textColor="@color/white" android:textSize="18sp" android:textStyle="bold" /> </LinearLayout> </FrameLayout> <!-- 功能列表区域 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:background="@drawable/bg_border" android:orientation="vertical" android:paddingStart="16dp" android:paddingEnd="16dp"> <!-- 我的订单 --> <LinearLayout android:id="@+id/myrecord" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:orientation="horizontal" android:clickable="true" android:focusable="true" android:background="?attr/selectableItemBackground"> <!-- 添加点击反馈 --> <ImageView android:layout_width="24dp" android:layout_height="24dp" android:layout_marginEnd="8dp" android:src="@drawable/ic_order" android:scaleType="centerInside" app:tint="@color/text_hint" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="我的订单" android:textColor="@color/text_hint" android:textSize="14sp" android:textStyle="bold" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:src="@drawable/arrow_right" android:scaleType="centerInside" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divider" /> <!-- 移除冗余分割线(原代码中有重复分割线) --> <!-- 个人信息 --> <LinearLayout android:id="@+id/ll_info" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:orientation="horizontal" android:clickable="true" android:focusable="true" android:background="?attr/selectableItemBackground"> <ImageView android:layout_width="24dp" android:layout_height="24dp" android:layout_marginEnd="8dp" android:src="@drawable/user_icon" android:scaleType="centerInside" app:tint="@color/text_hint" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="个人信息" android:textColor="@color/text_hint" android:textSize="14sp" android:textStyle="bold" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:src="@drawable/arrow_right" android:scaleType="centerInside" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divider" /> <!-- 联系客服 --> <LinearLayout android:id="@+id/ll_contact" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:orientation="horizontal" android:clickable="true" android:focusable="true" android:background="?attr/selectableItemBackground"> <ImageView android:layout_width="24dp" android:layout_height="24dp" android:layout_marginEnd="8dp" android:src="@drawable/ic_contact" android:scaleType="centerInside" app:tint="@color/text_hint" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="联系客服" android:textColor="@color/text_hint" android:textSize="14sp" android:textStyle="bold" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:src="@drawable/arrow_right" android:scaleType="centerInside" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divider" /> <!-- 关于商店 --> <LinearLayout android:id="@+id/ll_about" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:orientation="horizontal" android:clickable="true" android:focusable="true" android:background="?attr/selectableItemBackground"> <ImageView android:layout_width="24dp" android:layout_height="24dp" android:layout_marginEnd="8dp" android:src="@drawable/ic_about" android:scaleType="centerInside" app:tint="@color/text_hint" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="关于商店" android:textColor="@color/text_hint" android:textSize="14sp" android:textStyle="bold" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:src="@drawable/arrow_right" android:scaleType="centerInside" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divider" /> <!-- 退出登录 --> <LinearLayout android:id="@+id/ll_logout" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:orientation="horizontal" android:clickable="true" android:focusable="true" android:background="?attr/selectableItemBackground"> <ImageView android:layout_width="24dp" android:layout_height="24dp" android:layout_marginEnd="8dp" android:src="@drawable/ic_logout" android:scaleType="centerInside" app:tint="@color/text_hint" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="退出登录" android:textColor="@color/text_hint" android:textSize="14sp" android:textStyle="bold" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:src="@drawable/arrow_right" android:scaleType="centerInside" /> </LinearLayout> </LinearLayout> </LinearLayout> </androidx.core.widget.NestedScrollView>这是我fragment_me.xml里的代码,根据你的提示,把它补充完整,我需要完整的代码
最新发布
10-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值