Android Color Banding
Many Android programmers encounter severe color banding on devices like the Nexus One. For example, here is a radial gradient in an app I wrote this morning.

There is an awful lot of misinformation out there, for example I see many people advocating dithering as a solution. Here is my app with dithering enabled:

This is better, but won’t look so hot with many bitmaps. The correct solution is to take note of the format. As shown in the above images, Android’s default format is PixelFormat.OPAQUE. You can change this in a few ways. One way is to create a custom theme and set android:windowBackground to a color.
Another solution is to programmatically call Window.setFormat(PixelFormat.RGBA_8888).
The end result:

Sample code:
public class MyActivity extends Activity {
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
// Eliminates color banding
window.setFormat(PixelFormat.RGBA_8888);
}
}
Are there any side effects to using this PixelFormat?
I suppose it might be a good idea to check if the device actually supports it, and it could make painting slower. If you don’t have any gradients or images, you don’t need it…but that would be a pretty boring app.
I tried using Window.setFormat(PixelFormat.RGBA_8888) but I am still getting some banding.
Can you post some code snippet that works for you?
Try setting setDither(true) from java
Hello, Eric,
Nice solution
I have to ask though, does this mean, that the default RGB_565 pixel config that Android uses, will be changed to ARGB_8888 for all loaded bitmaps?
If that’s the case, I can understand why dithering is recommended more often, as with 4-byte transparent color scheme, you’ll need double the memory for the same bitmap.
Otherwise, your approach should indeed be better than dithering.
Cheers,
Dimitar
“I suppose it might be a good idea to check if the device actually supports it”
How would you go about doing that?
Worked like a charm !
The dithering on/off never worked for me.
My issue was with gradient alpha channel for creating shade effect.
Your hint saved a lot of mine and my GUI designer’s work hours (because we already started creating images without the alpha and with fixed backgrounds, then the stripes for the faked background fill … what a nightmare)
Keep it up !
Cheers,
Tomislav