Image does not show completely white despite it

2019-02-23 04:10发布

问题:

For a splashscreen I use an image which contains a white background (pure white - checked in Photoshop). For some reason it shows a slight green bg vs. the activity's default white bg - as marked in the screenshot. Only in some devices, like the

I add this as single view in a frame layout to the activity:

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

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/splashscreen" />

</FrameLayout>

Any idea? I read about RGB888 vs. RGB565 issues, but couldn't find a proper solution.

Note: I sure could make change the white in the image to transparent, but would prefer to understand the problem and find a proper solution.

回答1:

This is really annoying. I wonder, why so few people encountered this problem as it should appear on all 32-bit displays.

You mentioned right about RGB888 format. The cause of the problem is that regardless of the original bitmap format in the APK files all bitmaps are compressed (in my case into indexed 256-color! wtf?).

I couldn't find the way to prevent it, so if anyone knows the proper solution, please tell.

However, I found two solutions for the white color problem.

1) Per-bitmap: Say, we have a drawable bitmap resource called @drawable/mypng, which causes the problem. We need to add an additional XML drawable drawable/mypng_nofilter.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/mypng"
    android:filter="false"
/>

and use @drawable/mypng_nofilter instead.

2) For entire activity: In activity onCreate method we need to add

getWindow().setFormat(PixelFormat.RGB_565);

Now the window has 16-bit color depth and all bitmaps appear "properly" white.

Again, I would prefer to have 32-bit color depth, but I don't know how to control compile-time image compression.



回答2:

I have solved this issue by changing the Bitmap.CompressFormat type to PNG instead of JPEG:

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.nouv);
Bitmap bitmap = (Bitmap)((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);


回答3:

I encountered the same problem with network loaded images. I solved the issue by disabling bitmap filter. Since I do not have the issue on ICS and newer devices, I turned off the bitmap filter only for pre-ICS devices. Following is the code I used (setting network loaded bitmap to the ImageView.)

BitmapDrawable bitmapDrawable = new BitmapDrawable(imageView.getContext().getResources(), bitmap);
bitmapDrawable.setFilterBitmap(false);
imageView.setImageDrawable(bitmapDrawable);


回答4:

As your image only have a picture in its middle, I would advise you to set:

android:scaleType="centerCrop"

It will crop some part of your picture, at left and right, but will use the whole screen...



回答5:

You can also define a background to your FrameLayout or your ImageView using the color of your picture (which seems to be F8FCF8):

android:background="#F8FCF8"