2

I was going through Android : Converting color image to grayscale and other similar answers, when I doubted whether it's possible to grayscale a coloured "background image" i.e., image set using android:background="". Is it possible? If yes, how? I have not got the answer even after several hours of extensive googling.

EDIT: I have found a hack for the problem, but it does not solve the problem completely. I have used

android:backgroundTint="#999999"
android:backgroundTintMode="multiply"

along with my code to set the background image.

Palak Jain
  • 591
  • 4
  • 14
  • no, by using `android:background=...`, it is not possible, but you can use `View#setBackground` method in java code and use a custom `Drawable` class – pskink Aug 06 '18 at 19:20

1 Answers1

2

Use an ImageView in your layout instead of trying to set the background to a drawable and then use this code to programmatically color the image to grayscale.

final ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
mImageView.setColorFilter(filter);

If you need help using an ImageView instead of background, please post your layout xml and java code for this scene.

CodeSmith
  • 1,545
  • 1
  • 12
  • 28