How to display the first Chinese Character of the contact name in the contact profile picture

Source: Internet
Author: User

In android4.4.3The first letter of the contact's profile picture is displayed by default, but Chinese characters are not supported, such:

If the first contact name is an English character (a-z | A-Z), the default Avatar displays the first letter.

If the first Chinese character is displayed when Chinese characters are supported, it will be happy.

Let's take a look at how to implement this small function by modifying the source code ~

Let's take a look at the process of loading a contact's profile picture first ~

The question of loading the contact profile picture is still very interesting. In Contacts, the ContactPhotoManager class (strictly speaking, it is a subclass of this class) is used to implement asynchronous loading of the profile picture.

This class also uses LruCache to cache images, which is quite powerful. You can check if you are interested in asynchronous image loading and slowing down.

Take the contact list on the home page as an example. The general calling process is as follows (only for contacts without an avatar set, that is, photoUri is null ):

DefaultContactListAdapter-> bindView ()
ContactEntryListAdapter-> buildQuickContact ()
ContactEntryListAdapter-> getDefaultImageRequestFromCursor ()
ContactPhotoManagerImpl-> loadPhoto ()-> provider: LetterTileDefaultImageProvider // note that the DEFAULT_AVATAR object is used.
LetterTileDefaultImageProvider-> applyDefaultImage ()
LetterTileDefaultImageProvider-> getDefaultImageForContact ()
LetterTileDrawable-> drawLetterTile ()-> firsr char: high

Before the drawLetterTile function executes drawText, isEnglishLetter is called to determine whether the first character of a string is an English character. If yes, the first letter is drawn;

Otherwise, use the default Avatar

    private static boolean isEnglishLetter(final char c) {        return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');    }

Through the above process analysis, we can determine that the isEnglishLetter function causes Chinese characters not to be drawn.

Well, let's modify this function. Don't talk nonsense, directly go to the Code ~

    private static boolean isEnglishLetter(final char c) {        return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || isChineseLetter(c);    }
    private static boolean isChineseLetter(final char c) {        return isChinese(String.valueOf(c));    }

As for the isChinese function implementation, the code will not post, interested can refer to my article to judge the character for Chinese, Japanese, Korean (http://www.cnblogs.com/Lefter/p/3804051.html)

After this transformation, we can have the default Avatar display the first Chinese character of the Chinese name!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.