ColorFilterCache.javaAPI DocAndroid 5.1 API2831Thu Mar 12 22:22:56 GMT


public final class ColorFilterCache extends Object
Cache of {@link ColorFilter}s for a given color at different alpha levels.

Fields Summary
private static final android.util.SparseArray
private final[]
Constructors Summary
private ColorFilterCache(int r, int g, int b)

        // Pre cache all 256 filter levels
        for (int i = 0x00; i <= 0xFF; i++) {
            int color = Color.argb(i, r, g, b);
            mFilters[i] = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
Methods Summary
public static color)
Get a ColorDimmer for a given color. Only the RGB values are used; the alpha channel is ignored in color. Subsequent calls to this method with the same color value will return the same cache.

color The color to use for the color filters.
A cache of ColorFilters at different alpha levels for the color.

        final int r =;
        final int g =;
        final int b =;
        color = Color.rgb(r, g, b);
        ColorFilterCache filters = sColorToFiltersMap.get(color);
        if (filters == null) {
            filters = new ColorFilterCache(r, g, b);
            sColorToFiltersMap.put(color, filters);
        return filters;
public level)
Returns a ColorFilter for a given alpha level between 0 and 1.0.

level The alpha level the filter should apply.
A ColorFilter at the alpha level for the color represented by the cache.

        if (level >= 0 && level <= 1.0) {
            int filterIndex = (int) (0xFF * level);
            return mFilters[filterIndex];
        } else {
            return null;