ImageView中2倍高分辨率图片被放大裁剪的解决方法咨询
1. 修正Drawable资源放置目录
你的80x80像素图是2倍密度图,必须放在res/drawable-xhdpi目录下(xhdpi对应2x倍率)。如果放错目录(比如默认的drawable目录,对应mdpi倍率),系统会按比例强制放大图片,导致显示异常。
2. 替换android:src为app:srcCompat
使用AndroidX提供的app:srcCompat替代原生android:src,它对各类图片资源的适配逻辑更完善,能避免不必要的缩放问题:
<ImageButton android:id="@+id/attachment_button" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginLeft="8dp" android:scaleType="fitCenter" app:srcCompat="@drawable/button_mobile_attachment" />
注意:使用该属性需要在布局根节点添加命名空间声明:xmlns:app="http://schemas.android.com/apk/res-auto"
3. 移除adjustViewBounds属性
你已经固定了ImageButton的40x40dp尺寸,adjustViewBounds="true"会试图让View根据图片宽高比调整自身大小,反而会干扰fitCenter的缩放逻辑。直接移除该属性,确保图片完整居中显示:
<ImageButton android:id="@+id/attachment_button" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginLeft="8dp" android:scaleType="fitCenter" android:src="@drawable/button_mobile_attachment" />
4. 检查图片本身的画布范围
如果图片边缘存在大量透明像素,即使画布尺寸是80x80,实际有效内容可能被系统缩放后看起来像被裁剪。用图片编辑工具(如Figma、Photoshop)裁剪掉多余透明区域,让有效内容占满整个80x80画布。
内容的提问来源于stack exchange,提问作者newest_tfriz
相关产品推荐
相关产品推荐

