问题
How do select color of contact badge. What algorithm using?
回答1:
It does not save. It uses the hashcode of the Contact name string to determine the color.
Example:
String name = "Harish";
int colors[] = new int[] { Color.RED, Color.GREEN, Color.BLUE};
int chosenColor = colors[Math.abs(name.hashCode()) % colors.length];
I learnt from this answer
回答2:
You can try a Color generator like this..
public class ColorGenerator {
public static ColorGenerator DEFAULT;
public static ColorGenerator MATERIAL;
static {
DEFAULT = create(Arrays.asList(
//your list of default tints
));
MATERIAL = create(Arrays.asList(
//your list of material colors
));
}
private final List<Integer> mColors;
private final Random mRandom;
public static ColorGenerator create(List<Integer> colorList) {
return new ColorGenerator(colorList);
}
private ColorGenerator(List<Integer> colorList) {
mColors = colorList;
mRandom = new Random(System.currentTimeMillis());
}
public int getRandomColor() {
return mColors.get(mRandom.nextInt(mColors.size()));
}
public int getColor(Object key) {
return mColors.get(Math.abs(key.hashCode()) % mColors.size());
}
}
来源:https://stackoverflow.com/questions/34751185/android-lollipop-contact-color