简体   繁体   中英

Android lollipop contact color

How do select color of contact badge. What algorithm using?

在此处输入图片说明

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

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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM