首页 > 代码库 > android 4.4.3上面,联系人的头像默认显示首字母,但是不支持中文字符,修改支持中文

android 4.4.3上面,联系人的头像默认显示首字母,但是不支持中文字符,修改支持中文

在android 4.4.3上面,联系人的头像默认显示首字母,但是不支持中文字符,如下图:

如果联系人名字的第一位是英文字符(a-z || A-Z),则默认头像将显示该首字母。

如果支持中文时显示第一个汉字,那就happy了。

那就看看如何通过修改源代码来实现这一小功能吧~

我们还是先了解下联系人头像加载的流程吧~

联系人头像加载这个问题还是很有意思的,在Contacts中使用ContactPhotoManager类(严格来讲是这个类的子类)来实现头像的异步加载。

这个类还使用了LruCache来缓存图片,相当的强大,对图像的异步加载和缓存有兴趣的同志们可以看看。

以主页面的联系人列表加载头像为例。大致的调用流程为(只针对没有设置头像的联系人,即photoUri是null):

DefaultContactListAdapter->bindView()
  ContactEntryListAdapter->buildQuickContact()
    ContactEntryListAdapter->getDefaultImageRequestFromCursor()
      ContactPhotoManagerImpl->loadPhoto()->provider:LetterTileDefaultImageProvider // 注意,使用的是DEFAULT_AVATAR对象
        LetterTileDefaultImageProvider->applyDefaultImage()
          LetterTileDefaultImageProvider->getDefaultImageForContact()
            LetterTileDrawable->drawLetterTile()->firsr char:高

在drawLetterTile函数执行drawText之前会调用isEnglishLetter来判断字符串的首字符是否为英文字符,如果是,则将首字母画上去;



否则,使用默认头像

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

通过上面的流程解析,我们可以确定,是isEnglishLetter函数导致在中文字符不被描画。

嗯,那我们就改造一下这个函数吧。不废话,直接上代码~

    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));
    }

至于isChinese函数的实现,代码就不贴了,有兴趣的可以参考我的一篇判断字符为中文、日文、韩文的文章(http://www.cnblogs.com/Lefter/p/3804051.html)

经过这个改造后,我们就可以让默认头像显示中文名字的第一个汉字了!



具体修改如下。严重OK

    private static boolean isEnglishLetter(final char c) {
        return (‘A‘ <= c && c <= ‘Z‘) || (‘a‘ <= c && c <= ‘z‘);
    }
    
    private static boolean isChineseLetter(final char c) {
        return isChinese(c);
    }
    
    private  static  boolean isChinese(char c) {  
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
            return true;  
        }  
        return false;  
    } 
    
   //具体路径 com.android.contacts.common.lettertiles; LetterTileDrawable.java
     
     private void drawLetterTile(final Canvas canvas) {

        // Draw background color.
        sPaint.setColor(pickColor(mIdentifier));

        sPaint.setAlpha(mPaint.getAlpha());
        canvas.drawRect(getBounds(), sPaint);

        // Draw letter/digit only if the first character is an english letter
        if (mDisplayName != null && (isEnglishLetter(mDisplayName.charAt(0)) || isChineseLetter(mDisplayName.charAt(0)))) {
            // Draw letter or digit.
            sFirstChar[0] = Character.toUpperCase(mDisplayName.charAt(0));

            // Scale text by canvas bounds and user selected scaling factor
            final int minDimension = Math.min(getBounds().width(), getBounds().height());
            sPaint.setTextSize(mScale * sLetterToTileRatio * minDimension);
            //sPaint.setTextSize(sTileLetterFontSize);
            sPaint.getTextBounds(sFirstChar, 0, 1, sRect);
            sPaint.setColor(sTileFontColor);
            final Rect bounds = getBounds();

            // Draw the letter in the canvas, vertically shifted up or down by the user-defined
            // offset
            canvas.drawText(sFirstChar, 0, 1, bounds.centerX(),
                    bounds.centerY() + mOffset * bounds.height() + sRect.height() / 2,
                    sPaint);
        }else {
            // Draw the default image if there is no letter/digit to be drawn
            final Bitmap bitmap = getBitmapForContactType(mContactType);
            drawBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(),
                        canvas);
        }
    
     }
   
          //为什么头像的背景会有8种颜色   
   <!-- Make sure to also update LetterTileProvider#NUM_OF_TILE_COLORS when adding or removing
    colors -->
    <array name="letter_tile_colors">
        <item>#33b679</item>
        <item>#536173</item>
        <item>#855e86</item>
        <item>#df5948</item>
        <item>#aeb857</item>
        <item>#547bca</item>
        <item>#ae6b23</item>
        <item>#e5ae4f</item>
    </array>
     
    /** This should match the total number of colors defined in colors.xml for letter_tile_color */
    private static final int NUM_OF_TILE_COLORS = 8;  //八种颜色随机生成
    
    //获取颜色
    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()) % NUM_OF_TILE_COLORS;
        return sColors.getColor(color, sDefaultColor);
    }

android 4.4.3上面,联系人的头像默认显示首字母,但是不支持中文字符,修改支持中文