Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent part of image in ImageView become black

Tags:

I have problem when displaying image with transparency in Android KitKat (Nexus 7), it is OK in nexus 4 (KitKat) and other previous Android OS, here the image:

enter image description here

and ImageView layout:

<ImageView
android:id="@+id/avatar"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="21dp"
android:padding="3dp"
android:src="@drawable/icon_button_profile_new"
android:tag="@string/avatar" />

and here the screenshot when running on Nexus 7 (Android 4.4) enter image description here

also, I use Picasso for download & caching image from the URL.

like image 998
hakim Avatar asked Dec 05 '13 09:12

hakim


People also ask

Which method from the ImageView class allows you to adjust the transparency of an image?

image); image. setAlpha(150); // Value: [0-255]. Where 0 is fully transparent // and 255 is fully opaque. Set the value according // to your choice, and you can also use seekbar to // maintain the transparency.

What is translucent background?

A transparent background is usually a faded background with the main image, such as a logo, in the front. Such a design with the transparent background looks unique and fresh. Therefore, it becomes a branding tool, as well. You can use many tools, such as Photoshop to create transparent backgrounds.


1 Answers

After some trial: first I try using the image as resource drawable and it still happen (transparent part of the image become black), secondly I convert the image to png image and it is work, so the problem is in the file type (gif). since in my real app the image obtained from server and I can't request image as always in png format, I use the solution from this link: Transparent GIF in Android ImageView

it is simple for displaying only one image (like in my question) since I use Picasso I use target to erase black color from image avatar like this:

target = new Target() {         
@Override
public void onPrepareLoad(Drawable arg0) {

}

@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
    if (Build.VERSION.SDK_INT == 19/*Build.VERSION_CODES.KITKAT*/){
        Bitmap editedavatar = AndroidUtils.eraseColor(bitmap, -16777216);
        avatar.setImageBitmap(editedavatar);
    }
}

@Override
public void onBitmapFailed(Drawable arg0) {
    avatar.setImageResource(R.drawable.ic_profile_default);

where erase color is static method

public static Bitmap eraseColor(Bitmap src, int color) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap b = src.copy(Config.ARGB_8888, true);
    b.setHasAlpha(true);

    int[] pixels = new int[width * height];
    src.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < width * height; i++) {
        if (pixels[i] == color) {
            pixels[i] = 0;
        }
    }

    b.setPixels(pixels, 0, width, 0, 0, width, height);

    return b;
}

but since I use Picasso for displaying images in a listview,I impelements Target in ViewHolder and it is work very well so far.

like image 162
hakim Avatar answered Oct 14 '22 04:10

hakim