Android:获取联系背景颜色的关键是什么?

时间:2016-06-04 18:55:43

标签: android colors uicolor

所以我一直在阅读如何创建自动生成的联系人背景颜色。显然,它基于联系人中密钥的hashCode()。我已经看到它说电子邮件被用作密钥,但这没有任何意义,因为并非我的所有联系人都有与之关联的电子邮件,而且并非所有联系人都没有相同的颜色。

最终,我希望能够获得联系人卡片中使用的精确颜色。这样,我在我的应用程序中的图标具有与单击它时使用的相同的背景颜色,并使用ACTION_VIEW打开联系人卡片。

那么,只是想知道我需要使用什么作为关键来为每个联系人生成由android联系人应用程序生成的相同颜色?感谢。

PS。这是我现在用于颜色味觉的十六进制代码。如果有人也可以说明这一点的准确性,我会非常感激。感谢。

<array name="letter_tile_colors">
        <item>#f16364</item>
        <item>#f58559</item>
        <item>#f9a43e</item>
        <item>#e4c62e</item>
        <item>#67bf74</item>
        <item>#59a2be</item>
        <item>#2093cd</item>
        <item>#ad62a7</item>
    </array>

编辑:有些人一直在说它与另一个答案相似,Android lollipop contact color

答案的问题是它不完整。它解释了如何以相同的方式生成颜色,但我不只是尝试进行随机颜色生成。我希望获得默认联系人应用为该联系人使用的确切颜色。

2 个答案:

答案 0 :(得分:3)

From Google ContactsCommon source code:

The identifier is a unique and deterministic string that can be used to identify this contact. This is usually the contact's lookup key, but other contact details can be used as well, especially for non-local or temporary contacts that might not have a lookup key. This is used to determine the color of the tile. From ContactPhotoManager.

The identifier is used LetterTileDrawable class to select the tile color (identifier comes from contact request).

/**
 * Returns a deterministic color based on the provided contact identifier string.
 */
private int pickColor(final String identifier) {
    if (TextUtils.isEmpty(identifier) || mContactType == TYPE_VOICEMAIL) {
        return sDefaultColor;
    }
    // String.hashCode() implementation is not supposed to change across java versions, so
    // this should guarantee the same email address always maps to the same color.
    // The email should already have been normalized by the ContactRequest.
    final int color = Math.abs(identifier.hashCode()) % sColors.length();
    return sColors.getColor(color, sDefaultColor);
}

Palettes are defined in colors.xml file:

<!-- Background colors for LetterTileDrawables. This set of colors is a subset of
    https://spec.googleplex.com/quantumpalette#extended which passes Google Accessibility
    Requirements for the color in question on white with >= 3.0 contrast. We used
    http://leaverou.github.io/contrast-ratio/#white-on-%23db4437 to double-check the contrast.
    These colors are also used by MaterialColorMapUtils to generate primary activity colors.
-->
<array name="letter_tile_colors">
    <item>#DB4437</item>
    <item>#E91E63</item>
    <item>#9C27B0</item>
    <item>#673AB7</item>
    <item>#3F51B5</item>
    <item>#4285F4</item>
    <item>#039BE5</item>
    <item>#0097A7</item>
    <item>#009688</item>
    <item>#0F9D58</item>
    <item>#689F38</item>
    <item>#EF6C00</item>
    <item>#FF5722</item>
    <item>#757575</item>
</array>
<!-- Darker versions of letter_tile_colors, two shades darker. These colors are used
    for settings secondary activity colors. -->
<array name="letter_tile_colors_dark">
    <item>#C53929</item>
    <item>#C2185B</item>
    <item>#7B1FA2</item>
    <item>#512DA8</item>
    <item>#303F9F</item>
    <item>#3367D6</item>
    <item>#0277BD</item>
    <item>#006064</item>
    <item>#00796B</item>
    <item>#0B8043</item>
    <item>#33691E</item>
    <item>#E65100</item>
    <item>#E64A19</item>
    <item>#424242</item>
</array>
<!-- The default color used for tinting photos when no color can be extracted via Palette,
        this is Blue Grey 500 -->
<color name="quickcontact_default_photo_tint_color">#607D8B</color>
<!-- The default secondary color when no color can be extracted via Palette,
        this is Blue Grey 700 -->
<color name="quickcontact_default_photo_tint_color_dark">#455A64</color>
<color name="letter_tile_default_color">#cccccc</color>
<color name="letter_tile_font_color">#ffffff</color>

答案 1 :(得分:-1)

希望这可以帮到你

 private static final int NUM_OF_TILE_COLORS = 8;
 private final TypedArray mColors;

    //initialize inside oncreate of your activity
    mColors = res.obtainTypedArray(R.array.letter_tile_colors);//array of colors you have

    //call in wherver you want, key is the name of the contact
    private int pickColor(String key) {
    // String.hashCode() is not supposed to change across java versions, so
    // this should guarantee the same key always maps to the same color
    final int color = Math.abs(key.hashCode()) % NUM_OF_TILE_COLORS;
    try {
        return mColors.getColor(color, Color.BLACK);
    } finally {
        mColors.recycle();
    }
}