Android: อะไรคือกุญแจสำคัญในการรับสีพื้นหลังของผู้ติดต่อ?

ดังนั้นฉันจึงได้อ่านเกี่ยวกับวิธีการสร้างสีพื้นหลังที่สร้างขึ้นโดยอัตโนมัติสำหรับผู้ติดต่อ เห็นได้ชัดว่ามันขึ้นอยู่กับ hashCode() ของคีย์ในผู้ติดต่อ ฉันเห็นข้อความบอกว่าอีเมลถูกใช้เป็นกุญแจ แต่ก็ไม่สมเหตุสมผล เนื่องจากผู้ติดต่อของฉันบางรายเท่านั้นที่มีอีเมลเชื่อมโยงอยู่ และอีเมลที่มีสีไม่เหมือนกันทั้งหมด

ท้ายที่สุดแล้ว ฉันต้องการให้ได้สีที่แน่นอนที่ใช้ในการ์ดของผู้ติดต่อ ด้วยวิธีนี้ ไอคอนที่ฉันมีในแอปจะมีสีพื้นหลังเหมือนกับที่จะใช้เมื่อคุณคลิกและเปิดบัตรข้อมูลที่ติดต่อโดยใช้ ACTION_VIEW

ดังนั้นแค่สงสัยว่าฉันต้องใช้อะไรเป็นกุญแจในการสร้างสีเดียวกับที่สร้างโดยแอพผู้ติดต่อ 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

ปัญหาของคำตอบนั้นคือมันไม่สมบูรณ์ มันอธิบายวิธีการสร้างสีในลักษณะเดียวกัน แต่ฉันไม่ได้แค่พยายามสร้างสีแบบสุ่มเท่านั้น ฉันต้องการสีที่แน่นอนที่แอปผู้ติดต่อเริ่มต้นใช้สำหรับผู้ติดต่อนั้น


person user2223059    schedule 04.06.2016    source แหล่งที่มา
comment
สีของรายชื่อติดต่อ Android lollipop ที่เป็นไปได้   -  person GreyBeardedGeek    schedule 04.06.2016
comment
มันไม่ใช่. คำถามนั้นให้สูตรที่ฉันใช้เพื่อเลือกรหัสฐานสิบหกจากรายการ แต่ไม่ได้ระบุวิธีรับสีที่แน่นอนที่ใช้โดยแอปผู้ติดต่อ   -  person user2223059    schedule 05.06.2016
comment
ปัญหาคือการได้รายการสีที่แน่นอน หากใช้วิธีการข้างต้น หรือเพื่อค้นหาวิธีอื่นสำหรับสีเดียวกัน มีความคิดอะไรบ้าง?   -  person Deepak Negi    schedule 05.07.2016
comment
@DeepakNegi ตรวจสอบคำตอบของฉัน   -  person Gustavo Morales    schedule 12.07.2016


คำตอบ (2)


จากซอร์สโค้ด ContactsCommon ของ Google:

ตัวระบุเป็นสตริงเฉพาะและกำหนดขึ้นซึ่งสามารถใช้เพื่อระบุผู้ติดต่อรายนี้ โดยปกติจะเป็นคีย์การค้นหาของผู้ติดต่อ แต่สามารถใช้รายละเอียดผู้ติดต่ออื่นๆ ได้เช่นกัน โดยเฉพาะสำหรับผู้ติดต่อที่ไม่ใช่ในพื้นที่หรือผู้ติดต่อชั่วคราวที่อาจไม่มีคีย์การค้นหา ใช้เพื่อกำหนดสีของกระเบื้อง จาก ติดต่อ PhotoManager

ตัวระบุถูกใช้ LetterTileDrawable คลาสเพื่อเลือกสีไทล์ (ตัวระบุมาจากคำขอผู้ติดต่อ)

/**
 * 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>
person Gustavo Morales    schedule 11.07.2016

หวังว่านี่อาจช่วยคุณได้

 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();
    }
}
person SaravInfern    schedule 10.07.2016
comment
น่าเสียดายที่มันไม่ได้ ส่วนนั้นฉันคิดออกแล้ว ค่านิยมหลักที่ทำให้เกิดปัญหา Google ใช้คีย์ใดสำหรับค่าที่อยู่ติดต่อ - person user2223059; 10.07.2016