色值的计算、转换、获取

1、为颜色设置透明度

/**
* Get the value of color with specified alpha.
* @param color
* @param alpha between 0 to 255.
* @return Return the color with specified alpha.
*/
public static int getColorAtAlpha(int color, int alpha) {
if (alpha < 0 || alpha > 255) {
throw new IllegalArgumentException("The alpha should be 0 - 255.");
}
return Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
}

public static int getColor(int baseColor, float alphaPercent){
int alpha = Math.round(Color.alpha(baseColor) * alphaPercent);
return (baseColor & 0x00FFFFFF) | (alpha << 24);
}

2、将字符串色值转为int

  public static int getColorFromStr(String colorString){
        int color = 0;
        int startIndex = 0;
        if(colorString.contains("#")){
            startIndex = colorString.indexOf("#");
            color = Color.parseColor(colorString.substring(startIndex));
        }else {
            color = Color.parseColor(colorString);
        }
        return color;
    }

 

posted @ 2017-07-05 19:22  Mr.xd  阅读(874)  评论(0)    收藏  举报