android - 当分辨率不正确时,drawable 会消失
android - drawable disappears when not in correct resolution
我有一个名为 background.xml
的休闲可绘制对象
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="@color/gradient_bottom"
android:endColor="@color/gradient_top"
android:type="linear" />
</shape>
</item>
</selector>
当xml在drawable文件夹下时,在某些可重现的情况下(不知道为什么,可能与高内存消耗有关)背景消失。
但是,当我重新创建场景并将相同的 xml 放入所有可绘制分辨率文件夹中时,背景不会消失(即 drawable-hdpi、drawable-xhdpi 等)
这种事情不应该发生,
有人可以解释为什么会发生这种情况以及如何更优雅地解决它吗?
P.S -
当我将纯色作为背景时,此问题不会发生
自定义按钮背景也会出现此问题,当我将自定义背景放入可绘制分辨率文件夹时已修复
我建议尽可能使用带有 dpi 限定符的资源目录。否则,您可能会在某些设备上遇到 OutOfMemory 错误。此处说明:
By default, Android scales your bitmap drawables (.png, .jpg, and .gif files) and Nine-Patch drawables (.9.png files) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the bitmaps. To ensure your bitmaps look their best, you should include alternative versions at different resolutions for different screen densities.
At runtime, the system ensures the best possible display on the current screen with the following procedure for any given resource:
The system uses the appropriate alternative resource
Based on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with the hdpi qualifier (such as drawable-hdpi/) might be the best match, so the system uses the drawable resource from this directory.
If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density
The "default" resources are those that are not tagged with a configuration qualifier. For example, the resources in drawable/ are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium-density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate.
However, when the system is looking for a density-specific resource and does not find it in the density-specific directory, it won't always use the default resources. The system may instead use one of the other density-specific resources in order to provide better results when scaling. For example, when looking for a low-density resource and it is not available, the system prefers to scale-down the high-density version of the resource, because the system can easily scale a high-density resource down to low-density by a factor of 0.5, with fewer artifacts, compared to scaling a medium-density resource by a factor of 0.75.
我有一个名为 background.xml
的休闲可绘制对象<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="@color/gradient_bottom"
android:endColor="@color/gradient_top"
android:type="linear" />
</shape>
</item>
</selector>
当xml在drawable文件夹下时,在某些可重现的情况下(不知道为什么,可能与高内存消耗有关)背景消失。
但是,当我重新创建场景并将相同的 xml 放入所有可绘制分辨率文件夹中时,背景不会消失(即 drawable-hdpi、drawable-xhdpi 等)
这种事情不应该发生, 有人可以解释为什么会发生这种情况以及如何更优雅地解决它吗?
P.S -
当我将纯色作为背景时,此问题不会发生
自定义按钮背景也会出现此问题,当我将自定义背景放入可绘制分辨率文件夹时已修复
我建议尽可能使用带有 dpi 限定符的资源目录。否则,您可能会在某些设备上遇到 OutOfMemory 错误。此处说明:
By default, Android scales your bitmap drawables (.png, .jpg, and .gif files) and Nine-Patch drawables (.9.png files) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the bitmaps. To ensure your bitmaps look their best, you should include alternative versions at different resolutions for different screen densities.
At runtime, the system ensures the best possible display on the current screen with the following procedure for any given resource:
The system uses the appropriate alternative resource Based on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with the hdpi qualifier (such as drawable-hdpi/) might be the best match, so the system uses the drawable resource from this directory.
If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density The "default" resources are those that are not tagged with a configuration qualifier. For example, the resources in drawable/ are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium-density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate. However, when the system is looking for a density-specific resource and does not find it in the density-specific directory, it won't always use the default resources. The system may instead use one of the other density-specific resources in order to provide better results when scaling. For example, when looking for a low-density resource and it is not available, the system prefers to scale-down the high-density version of the resource, because the system can easily scale a high-density resource down to low-density by a factor of 0.5, with fewer artifacts, compared to scaling a medium-density resource by a factor of 0.75.