Programmatic Alpha Transparency Change for ImageViews on Android.

Sometimes there is a use case for programmatically changing the Alpha transparency of an image on Android. Perhaps you want to denote an email as read or some other case where you want to de-emphasize content.

It's relatively straightforward using the following two functions:

 /**
     * Adjust the saturation of an ImageView.
     * @param view The ImageView to adjust.
     * @param saturation The floating point saturation value. 1.0f is full saturation, 0f is grayscale.
     */
    public static void adjustSaturation(ImageView view, float saturation) {
        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(saturation);
        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
        view.setColorFilter(filter);
    }

    /**
     * Render in ImageView as grayscale.
     * @param view  The view to modify.
     */
    public static void makeGrayscale(ImageView view) {
        adjustSaturation(view, 0f);
    }

Additionally, many other UI widgets have a .setAlpha method, so it's even more straightforward to modify the transparency of a TextView...etc.

BLAUHAUS Software
605 The Woods
Cherry Hill, NJ
(215)668-1062
sqh@me.com

The Blauhaus, a pun on Bauhaus, the famous school of design, was the fine arts building at Penn c/a 1995. The home of motorcycles in the hallways, and not just one gunfight, it was a center of creativity and experimentalism, a home for radical artist/academics finding their voice amid the dubious commercialism and conservative politics of the university at the time.



Fill out my online form.