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

时间:2017-07-07 10:09:35

标签: android colors imageview picasso

如何在ImageView中用透明色(或其他颜色)替换黑色?

我使用Picasso从网上加载图片:

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

目前它看起来像这样:

enter image description here

图像本身包含我要删除的黑色背景。我尝试使用myImageView.setColorFilter(Color.BLACK);,但它似乎无效。

2 个答案:

答案 0 :(得分:1)

你可以试试这个。

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

        }
    });

答案 1 :(得分:-1)

Andy Developer's answer有所帮助,但我必须手动将黑色像素转换为我喜欢的像素。虽然我不确定这是否是最佳方式,但它仍然有效。

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) { }
});
相关问题