Method returning color with modified alpha proportional to given values. - Android Graphics

Android examples for Graphics:Color Value

Description

Method returning color with modified alpha proportional to given values.

Demo Code


//package com.java2s;
import android.graphics.Color;

public class Main {
    /**//from ww w . j a v a2s .c  o  m
     * Method returning color with modified alpha proportional to given values.
     *
     * @param color     color to modify
     * @param fullValue total value
     * @param partValue part of fullValue. When partValue equals fullValue, the alpha is 255.
     * @return color with alpha relative to partValue/fullValue ratio.
     */
    public static int getColorWithProportionalAlpha(int color,
            float fullValue, float partValue) {
        float progress = Math.min(Math.max(partValue, 0), fullValue)
                / fullValue;
        return Color.argb(Math.round(Color.alpha(color) * progress),
                Color.red(color), Color.green(color), Color.blue(color));
    }
}

Related Tutorials