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

问题描述 投票:-1回答:2

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

实际图片:enter image description here工作室里面的图片:enter image description here

以下是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>
java android imagebutton
2个回答
2
投票

您实际上是在/mipmap目录中引用像Launcher图标这样的图像。

我建议缩小你只使用ImageButton的图像大小,并将其放在/drawable中,你想要的质量或者可能小于当前的质量,而不是放在/mipmap目录中。

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


2
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.