I am using a few of the stock Android resources, such as ic_menu_camera.png
:
The images have a transparent background (desired), but also some transparency in the coloured pixels, too (undesired).
I'm using a ColorMatrixColorFilter to apply a tint to these images and that's working fine, however, the small amount of transparency in the icons is causing the underlying background to bleed through, and fading the colour. I can't figure out a programmatic way to set all coloured pixels to be opaque. Any help?
Current colouring code:
public static void colorImageView(Context context, ImageView imageView, @ColorRes int colorResId) {
Drawable drawable = imageView.getDrawable();
int color = context.getResources().getColor(colorResId);
drawable.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(new float[] {
0, 0, 0, 0, Color.red(color),
0, 0, 0, 0, Color.green(color),
0, 0, 0, 0, Color.blue(color),
0, 0, 0, 1, 0,
})));
}
Current result:
(first icon's source image is opaque, whereas the other 3 have undesired transparency, leading to this off-blue colour)
A very simple approach came to my mind first: Maybe you could convert the Drawable into a Bitmap and perform the desired pixel operations (like in this answer), e.g:
for(int x = 0; x < bitmap.getWidth(); x++) {
for(int y = 0; y < bitmap.getHeight(); y++) {
int pixel = bitmap.getPixel(x, y);
int r = Color.red(pixel), g = Color.green(pixel), b = Color.blue(pixel);
if (r != 0 || g != 0 || b != 0)
{
pixel.setPixel(x, y, Color.rgb(r, g, b));
}
}
}
to delete any alpha channel value in each pixel with an r-, g- or b-value higher than zero.
But I dont know, how poor or slow this approach works. I think the conversion into a bitmap with pixel operations might be much slower than a ColorMatrixColorFilter.