Android lollipop contact color

江枫思渺然 提交于 2019-12-12 10:17:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!