Lendl Leyba
Lendl Leyba

Reputation: 2287

PNG in ImageView has black background

My imageview is set to a bitmap that is a PNG. But problem is that the transparent part is black. I've set background to @android:color/transparent and #00000000, but it is still black. I've tried to search but nothing helped. Thanks in advance!

Here's are the xml of the imageView with it's parents:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/mainbg"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/vbTempLink"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="15"
        android:background="@color/mainbg"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight="10"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/tempImage1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="25"
                android:background="@android:color/transparent"
                android:gravity="center"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:scaleType="fitCenter" />

I set the png with these codes (bg is string):

background.setBackgroundDrawable(bitmapToDrawable((bg)));

public Drawable bitmapToDrawable(String s) {
    Bitmap bitmap = BitmapFactory.decodeFile(path + s);
    Drawable drawable = new BitmapDrawable(getResources(), bitmap);
    return drawable;
}

And yes the image surely has a transparent background.

Upvotes: 3

Views: 9962

Answers (3)

Praveen Balaji
Praveen Balaji

Reputation: 387

I know this is very old post but this might help others..

From your code I can clearly see that you are retrieving your image from local storage and not from Android resources.

Bitmap bitmap = BitmapFactory.decodeFile(path + s);

So I think you are trying to retrieve JPEG compressed image. File extensions are meaningless if it has been stored with some other compression formats.

If you want to store and retrieve PNG images then store your source images in PNG format and then retrieve it.

bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

Because by default JPEG compression would store transparent background pixels in black color. So better avoid compressing it as JPEG.

Upvotes: 6

Ssr1369
Ssr1369

Reputation: 358

I assume that if you want the png background to be transparent is because there is something else behind. Then, I made the transparent stuff work with this:

Drawable[] layers = new Drawable[2];
layers[0]=resources.getDrawable(colour);
layers[1]=resources.getDrawable(R.drawable.somethingToBeDrawn);
(someView).setImageDrawable(new LayerDrawable(layers));

where colour is the picture from behind, in my case a green or grey colour's png (that's way it's called "colour").

Upvotes: 0

Lendl Leyba
Lendl Leyba

Reputation: 2287

Turned out that it was on how i compressed the bitmap.

bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

No other way but to find another way. Thanks guys!

Upvotes: 2

Related Questions