What am I missing when trying to apply a grayscale filter to ImageView?

48 views Asked by At

According to multiple sources this code should work on an ImageView. An ImageButton is a child class of an ImageView, so this should not be a factor. Just in case, I have tried changing the view to an ImageView. Either way, there is no effect and the image remains in full color.

ImageButton imageButton = findViewById(R.id.imageButton);

ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);

ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageButton.setColorFilter(filter);

1

There are 1 answers

0
F.K. Juliano On

I found the problem: my view did not have an src, only a background, which is to say, it had:

android:background="@drawable/jw_true"

instead of:

android:src="@drawable/jw_true"

These are interchangeable for many purposes, but not for this. When my attempts were not working, the filter was being applied to the non-existent src, which is why nothing happened.