ImageView使用wrap_content时图片尺寸有缩放

本文介绍了如何解决在Android应用中图片显示模糊的问题。通过调整图片资源的放置位置和使用正确的图片资源文件夹,可以确保图片在不同分辨率的设备上正确显示。此外,还介绍了如何利用android:scaleType属性来控制图片的缩放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这两天在做一个项目时,发现UI team给的图片无论我怎么设置layout,图片总是模糊的,有缩放,即使给imageView设置width和height为图片的原始尺寸大小也不行。

后来想想只可能是android系统自身对图片进行了缩放,我之前是将所有的图片都放在res/drawable-hdpi文件夹中的。我的设置实际分辨率是1024*600,考虑到这一点,属于mdpi,于是我将所有的图片都移到res/drawable-mdpi文件夹下,所有图片都清晰了。

现在将所有的imageView的布局都设置为wrap_content,效果很好,没有问题。


另外在布局图片时可以设置图片缩放的一个属性android:scaleType,可以依据期望的效果设置图片是否缩放,该如何缩放。

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content" android:background="#FFFFFF"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" tools:ignore="MissingConstraints"> <LinearLayout android:id="@+id/ll_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:ignore="MissingConstraints"> <LinearLayout android:id="@+id/ll_label" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" tools:ignore="MissingConstraints"> <!-- 返回按钮 --> <ImageView android:id="@+id/btn_back" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginStart="16dp" android:padding="8dp" android:src="@drawable/img_back" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <!-- 分享按钮 --> <ImageView android:id="@+id/btn_share" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginEnd="16dp" android:padding="8dp" android:src="@drawable/img_share" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </LinearLayout> <LinearLayout android:id="@+id/ll_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 主图片 - 调整高度为0dp实现自适应 --> <ImageView android:id="@+id/image_main" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="8dp" android:layout_marginEnd="16dp" android:scaleType="centerCrop" android:src="@drawable/xiaomi" app:layout_constraintBottom_toTopOf="@+id/details_container" app:layout_constraintDimensionRatio="H,16:9" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/phone_desc" app:layout_constraintTop_toBottomOf="@id/ll_label" /> <!-- 手机描述 - 移动到图片左侧 --> <LinearLayout android:id="@+id/phone_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:orientation="vertical" app:layout_constraintBottom_toTopOf="@+id/details_container" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/ll_label"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="名称:" /> <TextView android:id="@+id/phone_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小米14" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="价格:" /> <TextView android:id="@+id/phone_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥4399" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/spacs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="规格:" /> <TextView android:id="@+id/phone_spacs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="16G/256G" /> </LinearLayout> </LinearLayout> </LinearLayout> <!-- 详情内容容器 - 自适应屏幕宽度 --> <androidx.cardview.widget.CardView android:id="@+id/details_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:layout_marginBottom="24dp" app:cardCornerRadius="8dp" app:cardElevation="4dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/image_main"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小米14 详细介绍" android:textSize="18sp" android:textStyle="bold" android:paddingBottom="8dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="小米14搭载骁龙8 Gen3处理器,采用6.36英寸2K直屏,支持120Hz高刷新率,兼顾性能与续航。影像系统升级为5000万像素三摄,主摄使用索尼IMX900传感器,支持OIS光学防抖,拍照画质更上一层楼。" android:lineSpacingExtra="4sp" android:paddingBottom="12dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="产品特点" android:textSize="16sp" android:textStyle="bold" android:paddingTop="16dp" android:paddingBottom="4dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 性能:骁龙8 Gen3 + LPDDR5X + UFS 4.0,安兔兔跑分超200万" android:paddingBottom="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 屏幕:6.36英寸华星光电C8直屏,2K分辨率,120Hz刷新率,2160Hz高频PWM调光" android:paddingBottom="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 影像:5000万主摄(IMX900)+5000万超广角+5000万人像,OIS+EIS双防抖" android:paddingBottom="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 续航:4610mAh电池+120W有线快充+50W无线快充,19分钟充满" android:paddingBottom="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- 系统:MIUI 15基于Android 14,支持光子引擎,流畅度提升30%" /> </LinearLayout> </LinearLayout> </androidx.cardview.widget.CardView> </LinearLayout> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout>这样设计为什么图片加载不出来
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值