Sviatoslav
Sviatoslav

Reputation: 1312

How to apply RGBA_8888 and dither correctly?

I have splash.png with gradient. But on screen this image looks not so good ...

My simple .apk for this question consists of:

public class TestditherActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

  @Override
  public void onAttachedToWindow() {
    super.onAttachedToWindow();
    getWindow().setFormat(PixelFormat.RGBA_8888);
  }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY"
    android:src="@drawable/test"/>

</LinearLayout>

and test.xml:

<?xml version="1.0" encoding="utf-8"?> 

<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/splash"
android:antialias="true"
android:dither="true" />

splash.png

source

result

enter image description here

How to apply RGBA_8888 and dither correctly? Where is my mistake?

The next code gives me perfect result in emulator, but not on the real device:

public class TestditherActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getWindow().setFormat(PixelFormat.RGBA_8888);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap gradient = BitmapFactory.decodeResource(getResources(), R.drawable.splash, options);

    findViewById(R.id.linear).setBackgroundDrawable(new BitmapDrawable(gradient));
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

Upvotes: 3

Views: 4879

Answers (1)

Jave
Jave

Reputation: 31846

Take a look at this: Awful background image quality in Android
Or this blog post about dithering in Android: http://android.amberfog.com/?p=247

In your case the problem is in the bitmap xml. I remember reading somewhere that the dither only works if the bitmap has a tileMode set.

Upvotes: 1

Related Questions