Search code examples
androidcolorsimageviewpicasso

Make ImageView display black portions of image as different color


How can I replace the black color with transparent color (or some other color) in an ImageView?

I load image from web using Picasso:

Picasso.with(getContext()).load("www.abc.com/a.png").into(myImageView);

Currently it looks like this:

enter image description here

The image itself contains the black background, which I want to remove. I tried using myImageView.setColorFilter(Color.BLACK); but it doesn't seem to work.


Solution

  • Andy Developer's answer helped, but I had to manually convert black pixels to pixels of my liking. It is working, although I am not sure if this is the best way.

    enter image description here

    Picasso.with(getContext()).load(IMAGE_URL).into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
        {
            Bitmap copy = bitmap.copy(bitmap.getConfig(),true);
    
            int [] allpixels = new int [copy.getHeight()*copy.getWidth()];
            copy.getPixels(allpixels, 0, copy.getWidth(), 0, 0, copy.getWidth(), copy.getHeight());
    
            int replacementColor = Color.parseColor("#34495E");
            for(int i = 0; i < allpixels.length; i++) {
                if(allpixels[i] == Color.BLACK)
                    allpixels[i] = replacementColor;
            }
    
            copy.setPixels(allpixels, 0, copy.getWidth(), 0, 0, copy.getWidth(), copy.getHeight());
    
            myImageView.setImageBitmap(copy);
        }
    
        @Override
        public void onBitmapFailed(Drawable errorDrawable) { }
    
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) { }
    });