使 ImageView 将图像的黑色部分显示为不同的颜色

Make ImageView display black portions of image as different color

如何将 ImageView 中的黑色替换为透明色(或其他颜色)?

我使用 Picasso 从网络加载图像:

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

目前看起来是这样的:

图像本身包含黑色背景,我想将其删除。我尝试使用 myImageView.setColorFilter(Color.BLACK);,但它似乎不起作用。

你可以试试这个。

Picasso.with(this).load("Your URL").into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
        {
           bitmap.eraseColor(Color.argb(AAA,RRR,GGG,BBB));
           OR
           bitmap.eraseColor(getResources().getColor(R.color.myColor));
           imageView.setBackground(new BitmapDrawable(bitmap));
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    });

有所帮助,但我必须手动将黑色像素转换为我喜欢的像素。它正在工作,虽然我不确定这是否是最好的方法。

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) { }
});