Apply ColorFilter / ColorMatrix to turn image completely white

1.4k Views Asked by At

I have a circular ProgressBar on my Toolbar to show some indeterminate progress happening, however the default style of the ProgressBar (the only one I know that makes it into a circular ring) is the same color as the toolbar (the 'colorPrimary' color).

The styles I tried are ?android:attr/progressBarStyleLarge and ?android:attr/progressBarStyleLargeInverse, both get the same color as the toolbar, hence they are not visible.

I read that I can change the color by applying a color filter as such:

toolbarProgress.getIndeterminateDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);

However, I cannot find a single 'PorferDuff.Mode' that turns a colored image into a completely white image (while keeping the alpha transparency intact).

Instead of using the PorterDuff modes I can also apply a ColorMatrix. For example to turn it into grayscale I can use this:

        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(new float[] {
            0.5f, 0.5f, 0.5f, 0, 0,
            0.5f, 0.5f, 0.5f, 0, 0,
            0.5f, 0.5f, 0.5f, 0, 0,
            0, 0, 0, 1, 0
    });
    toolbarProgress.getIndeterminateDrawable().setColorFilter(filter);

Same problem here: I can't find the correct matrix that turns the image completely white. Grayscale comes close but it turns into a grayish color and not pure white.

What I want is to turn all colored pixels into pure white, and leave all alpha pixels transparent.

Unless there is an easier way to change the color of the native ProgressBar..?

1

There are 1 best solutions below

3
On BEST ANSWER

Does it have to be ColorMatrixColorFilter? You can use DrawableCompat to tint Drawables.

Drawable drawable = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
DrawableCompat.setTint(drawable, Color.WHITE);
toolbarProgress.setIndeterminateDrawable(drawable);