Android仿照钉钉的人名头像

需求:项目中要实现类似钉钉呢种使用用户名的后两位当做头像,背景根据一定的规则显示不同的颜色。

主要用到了pinyin4j-2.5.0.jar包,获取中文名称的首字母根据一定的规则显示背景颜色;先看看功能截图:

Android仿照钉钉的人名头像_第1张图片
用到的工具类PinYinUtils

import android.text.TextUtils;
import android.widget.TextView;

import com.yxkj.yxapp.R;

import net.sourceforge.pinyin4j.PinyinHelper;

/**
 * Created by zfg on 2017/8/1.
 */

public class PinYinUtils {

    /**
     * 得到中文首字母
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        convert = convert.toUpperCase();
        return convert;
    }

    /**
     * 得到中文首字母
     *
     * @param str
     * @return
     */
    public static int getPinYinHead(String str) {
        char[] chars;
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        convert = convert.toUpperCase();
        chars = convert.toCharArray();

        if ((chars[0] >= 'A') && (chars[0] < 'G')) {
            return 1;
        } else if ((chars[0] >= 'G') && (chars[0] < 'N')) {
            return 2;
        } else if ((chars[0] >= 'N') && (chars[0] < 'T')) {
            return 3;
        } else if ((chars[0] >= 'T') && (chars[0] < 'Z')) {
            return 4;
        } else {
            return 5;
        }

    }

    public static void setTextbg(TextView textView, String username) {
        username = username.toString().trim();
        if (!TextUtils.isEmpty(username)) {
            int type = 1;
            if (username.length() >= 2) {
                textView.setText(username.substring(username.length() - 2, username.length()));
                type = PinYinUtils.getPinYinHead(username.substring(username.length() - 2, username.length()));
            } else {
                textView.setText(username);
                type = PinYinUtils.getPinYinHead(username);
            }
            switch (type) {
                case 1:
                    textView.setBackgroundResource(R.drawable.round_img1);
                    break;
                case 2:
                    textView.setBackgroundResource(R.drawable.round_img2);
                    break;
                case 3:
                    textView.setBackgroundResource(R.drawable.round_img3);
                    break;
                case 4:
                    textView.setBackgroundResource(R.drawable.round_img4);
                    break;
                case 5:
                  textView.setBackgroundResource(R.drawable.round_img5);
                    break;
            }
        } else {
            textView.setBackgroundResource(R.drawable.round_img1);
        }
    }
}

你可能感兴趣的:(android,工具类)