首页 > 解决方案 > 背景渐变导致色带

问题描述

我尝试在我的应用程序的背景上做一些线性渐变,结果出现了色带。

我做了什么:

我创建了一个名为window_background_app.xml的形状

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="45"
        android:endColor="#Fe005694"
        android:startColor="#fe2D8ACC"
        android:type="linear" />

</shape>

我在样式中使用了该形状以应用于背景:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@drawable/window_background_app</item>
    </style>

</resources>

我在我的应用程序清单中使用了这种风格:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:name="generic_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

问题

启动我的应用程序时,背景显示了一些带有渐变background_with_banding的条带

我在类似的问题(例如这个)中发现我应该:

我遇到了这个问题:

条带未出现在:

问题

我不确定问题是由于 Android 版本的更新还是来自材料(或两者)的更新。我遇到的另一个问题是,我可以在网上找到的很多解决方案都相当陈旧(最近的解决方案是在 2014 年左右)。

所以我的问题是:

自 dither=true 和 pixelFormat=RGBA_8888 以来,是否有一些新的渐变色带解决方案?

标签: androidlinear-gradients

解决方案


如果像素格式是 RGBA8888,我很确定抖动不会做任何事情,因此无法帮助视觉上相邻的 24 位 RGB 色调之间的条带。

这可能是 Android 的限制,它源于 OpenGL 的限制,即抖动仅适用于低于 32 位的位深度。OpenGL 是 Android 视图的支持实现。

因此,解决方案可能是使用 PNG 文件进行渐变,并且您的绘图程序已经内置了抖动功能。


推荐阅读