所以我一直在研究如何为联系人创建自动生成的背景颜色。显然它是基于联系人中某个键的 hashCode()。我看到它说电子邮件被用作 key ,但这没有意义,因为并非我所有的联系人都有与之关联的电子邮件,而且那些没有的电子邮件也不完全相同。
最终,我希望能够获得联系人名片中使用的准确颜色。这样,我的应用程序中的图标就具有与您单击它并使用 ACTION_VIEW 打开名片时所使用的背景色相同的背景色。
所以,只是想知道我需要使用什么作为 key 来为每个单独的联系人生成由 android 联系人应用程序生成的相同颜色?谢谢。
附言。这是我现在拥有的用于调色板的十六进制代码。如果有人也能对此的准确性发表意见,我将非常感激。谢谢。
<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
该答案的问题在于它不完整。它解释了如何以相同的方式生成颜色,但我不只是尝试随机生成颜色。我希望获得默认联系人应用程序为该联系人使用的确切颜色。
最佳答案
来自谷歌 ContactsCommon源代码:
identifier 是一个唯一且确定的字符串,可用于识别此联系人。这通常是联系人的查找键,但也可以使用其他联系人详细信息,尤其是对于可能没有查找键的非本地或临时联系人。这用于确定图 block 的颜色。来自 ContactPhotoManager .
使用标识符LetterTileDrawable用于选择图 block 颜色的类(标识符来自联系请求)。
/**
* 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);
}
调色板在 colors.xml 中定义文件:
<!-- 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>
关于Android:获取联系人背景色的关键是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633867/