android:dither="true" does not dither, what's wrong?
Asked Answered
H

5

21

I've been trying to get android to dither the background image of an activity - so far without success. I have no clue what's wrong.

This is what I did:

The root element of my activity layout is a LinearLayout:

<LinearLayout android:id="@+id/AbsoluteLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="center_horizontal"
android:background="@drawable/background_dither">

where I added @drawable/background_dither as the background image. I put an XML file "background_dither.xml" in drawable-hdpi with the following content:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/backimg"
android:src="@drawable/background"
android:dither="true"
android:antialias="true" />

which references the actual image background.png in drawable-hdpi. The image, which includes a large color gradient, does show up, but with heavy color banding. From what I've learned from the SDK, this can be mitigated by using the /above proxy image definition together with specifying android:dither="true". This however, has absolutely no effect.

What am I missing to get dithering working?

EDIT: Uploaded the sources here

EDIT2: After none of the suggested methods helped to get rid of color banding, after reading this blog post from Romain Guy I had the idea to check whether my PNG background has an alpha channel. It didn't. After adding one, android actually seems to use RGB8888, as said in the post and the banding is gone (also for 2.2). Still wondering why the other methods didn't work.

EDIT3: One has to make sure that the PNG not only has an alpha channel, but also at least one pixel that has an alpha value != FF, otherwise the android build tools will again strip that bitmap down to an indexed palette without alpha channel.

Hadji answered 22/1, 2011 at 18:58 Comment(0)
R
24

Ran into this problem too today and found and used your solution : Create the png in photoshop with transparency set AND having at least one pixel using the alpha. Actually what i did is set the layer to a 99% opacity. Doesn't affect the colors or anything and make the Android compiler leave my png alone.

Reputable answered 7/4, 2011 at 13:4 Comment(0)
S
3

Try to put this line

getWindow().setFormat(PixelFormat.RGBA_8888); 

just after super.onCreate() and see if it helps.

Selfconceit answered 23/1, 2011 at 0:38 Comment(1)
Thanks, but sorry, no, didn't see any difference. I uploaded the code here rapidshare.com/files/444144083/lottofee.zip Maybe someone can spot the problem.Hadji
B
2

I had this issues too, I fixed applying nine-patch feature to background image.

Hope this help

Barozzi answered 17/6, 2011 at 17:12 Comment(0)
G
0

A lot of folks having the same problem report that setting setDither(true) on your drawable resource in the Java code works when the XML attribute does not. In this case, if you can get a reference to your LinearLayout in the onCreate method, try

linearLayout.getBackground().setDither(true).

An alternative would be to try setting a different pixel format as described here:

https://web.archive.org/web/20180903190558/http://stuffthathappens.com/blog/2010/06/04/android-color-banding/

Good luck!

Gustaf answered 22/1, 2011 at 20:48 Comment(2)
Thanks. Tried both options. Neither of them change the rendering, so I guess it still doesn't work. :-(Hadji
The link is now brokenPheni
T
0

On Android 2.2.1 and below there is a bug which causes dithering to be disabled if filtering is enabled. On Android 2.3.3 and 3.0 this issue seems to be fixed. I'm not sure if the same might be true for antialias as you have enabled.

Tasman answered 3/5, 2011 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.