ImageView 在旧 Android 设备上像素化

ImageView is pixelated on older Android devices

问题

我的 XML 中有一个像这样的 ImageView:

                <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:src="@drawable/ic_person_add"
                android:alpha="0.87" />

ic_person_add是正常的Material设计Vectoricon.

在我的 Nexus 5 (Android 6) 上,图标缩放得很漂亮而且非常清晰:

然而,在 Note 2 (Android 4.4) 上,图标变得非常像素化和丑陋:

如何使 ImageView(以 Vector 作为 src)在每台设备上都清晰可见?最好只有 xml 解决方案。

我已经找到了:

Nexus 5 的密度桶是 xxhdpi,而 Note 2(基于 this)是 xhdpi。这表明您的 xxhdpi 位图大小合适(或者至少达到或超过所需大小),而 xhdpi 不合适。

如果您使用的是 Android studio 的最新版本提供的 automatic conversion,您可能想尝试手动光栅化矢量图像,看看是否可以解决问题。

如果您使用的是光栅图形,则可能需要向每个密度变体添加一些视觉元素,以了解实际使用的是哪个。

真正的问题不是 Note 2 的 DPI,而是它的 Android 版本。

Link Melllvar 发布状态:

Once you have a vectorDrawable image in your res/drawable, the Gradle plugin will automatically generate raster PNG images for API level 20 and below during build time

所以我的 Android 6 设备确实有一个 Vectorgraphic 作为 ImageView 的源,所以它成功地完美地放大了它。 对于 Note 2(运行 早于 Android 5 OS)Android Studio 自动将我的 Drawables 文件夹中的矢量图转换为 PNG。

您可以通过更改

在 VectorXML 中更改转换后的 PNG 的大小
android:height android:width

根据 dp 的大小,您的矢量图需要在 API 20 台设备上显示。所以在我的案例中(最大 100dp)我不得不将值更改为:

<vector android:height="100dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="100dp">