Is there a way I can gray out an ImageButton in Android without maintaining a separate gray copy of the original Image?

Tags: , , , ,



I am trying to have an ImageButton that initially has a gray effect on it & turns into its original color when selected. I’m trying to achieve something like this. I know this can be done in CSS, but was wondering if any Android attribute / function equivalent to it existed.

I read a post here that seems to suggest that I have to maintain a different gray copy of the same Image in order to achieve this. But it would consume a lot of memory to maintain 2 copies of the same Image if I have, lets say 100+ ImageButtons.

Unlike a regular Button, an ImageButton or a Button that has an image background is not grayed when disabled. You actually have to use another image or to process it in a way it appears grayed.

So is there really no way to achieve in Android what could be achieved using grayscale in CSS?

Thank You for your time!!

Answer

If you are using .png images, you could use the ImageView color filter attribute.

public void setDisabled(ImageView imageView) {
    final ColorMatrix grayscaleMatrix = new ColorMatrix();
    grayscaleMatrix.setSaturation(0);
    imageView.setColorFilter(new ColorMatrixColorFilter(grayscaleMatrix));
}


Source: stackoverflow