Skip to content

Instantly share code, notes, and snippets.

@BCsl
Last active November 1, 2016 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BCsl/f4b5f0b013c0e14e740351a14c4d84aa to your computer and use it in GitHub Desktop.
Save BCsl/f4b5f0b013c0e14e740351a14c4d84aa to your computer and use it in GitHub Desktop.
Easy way to generate ColorStateList
class ColorStateGenerate{
// 灰度
public static int greyer(int color) {
int blue = (color & 0x000000FF) >> 0;
int green = (color & 0x0000FF00) >> 8;
int red = (color & 0x00FF0000) >> 16;
int grey = Math.round(red * 0.299f + green * 0.587f + blue * 0.114f);
return Color.argb(0xff, grey, grey, grey);
}
// 明度
public static int darker(int color, float ratio) {
color = (color >> 24) == 0 ? 0x22808080 : color;
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= ratio;
return Color.HSVToColor(color >> 24, hsv);
}
public static ColorStateList csl(int normal, float ratio) {
// int disabled = greyer(normal);
int pressed = darker(normal, ratio);
int[][] states = new int[][]{{android.R.attr.state_pressed}, {}};
int[] colors = new int[]{pressed, normal};
return new ColorStateList(states, colors);
}
private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
int[] colors = new int[]{pressed, focused, normal, focused, unable, normal};
int[][] states = new int[6][];
states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
states[2] = new int[]{android.R.attr.state_enabled};
states[3] = new int[]{android.R.attr.state_focused};
states[4] = new int[]{android.R.attr.state_window_focused};
states[5] = new int[]{};
return new ColorStateList(states, colors);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment