为什么 ImageButton 质量模糊或降低到最差?

Why does ImageButton quality blurs or reduces to worst?

我的应用程序有一个质量太差的 ImageButton。实际图像尺寸为 4176*4176(4176 像素),单独打开时看起来完全没问题。但是,当我将其导入 android studio 并将其分配给 ImageButton 时,图像质量会下降。我一直在尝试多种方法来解决这个问题,但还没有成功。我完全不熟悉 Android Programming.I 转到 res/drawable 然后右键单击创建一个新的图像资源,然后指定导入 png 文件的位置。

实际图像: 工作室内部图片:

这是activity_main.xml中的代码:

<LinearLayout 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"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">

<ImageButton
    android:id="@+id/police"
    android:layout_width="317dp"
    android:layout_height="295dp"
    android:layout_centerInParent="true"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"

    android:background="@drawable/custom_button"
    />
</LinearLayout>

调用图片的自定义按钮代码:

<item
    android:state_pressed="true"
    android:drawable="@mipmap/pressed" />

<item
    android:drawable="@mipmap/defaultt" />
</selector>

我首先回顾 Android 上的密度独立像素。这是一个涵盖概念的好博客:https://www.captechconsulting.com/blogs/understanding-density-independence-in-android

主要问题是您的图像(非常高分辨率)正在被压缩以适合图像按钮的内部。通常,您会希望通过不同的可绘制对象资源目录(例如,drawable-hdpi、drawable-xhdpi、drawable-xxhdpi)包含不同密度的图像资源。

但是,现在您可以使用矢量绘图,这将减少每个密度桶拥有多个资产的需要,假设您能够生成 SVG 资产文件:https://developer.android.com/guide/topics/graphics/vector-drawable-resources

您实际上是在引用 /mipmap 目录中的启动器图标等图像。

我建议缩小您仅将其用于 ImageButton 的图像大小,并将其放置在 /drawable 中以您想要的质量或可能小于当前质量而不是将其放置在 /drawable 中=10=]目录。

此外,我实际上并没有看到 scaleType 如何帮助 ImageButton 的原因。您可能也想删除该行。