Android Studio防钉钉联系人列表(动态圆形文字头像)

钉钉联系人列表是:
包含汉字时候取后两个(不满两个字符去所有)
如:这片海 取:片海
不包含汉字时候取前两个(不满两个字符取所有)
如:sea 取 se
设计思路
画圆形图像,根据获得的名字进行在圆形图像的中心位置进行写字,背景颜色从颜色值中随机选择,然后通过Base64将图片转化为二进制进行本地数据保存。

颜色值(吸取钉钉上的6中颜色)

public static final String[] ImageBgColor={
            "#568AAD",
            "#17c295",
            "#4DA9EB",
            "#F2725E",
            "#B38979",
            "#568AAD"
    };

工具类(将文字转化为图片和base64转化)

public class BitmapUtil {

 /**
     * 这里是对头像进行动态绘制的
     * **/
    public static Bitmap GetUserImageByNickName(Context context,String nickname){
        Bitmap bitmap = Bitmap.createBitmap(80, 80, Bitmap.Config.ARGB_8888);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);// 抗锯齿
        Canvas canvas = new Canvas(bitmap);
        int a= (int)(Math.random()*6);
        paint.setColor(Color.parseColor(Contact.ImageBgColor[a]));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(40, 40, 40, paint);
        paint.setColor(Color.WHITE);
        // sp----->px单位变换
        int sp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,14, // 多少sp
                context.getResources().getDisplayMetrics());// 屏幕的密度
        paint.setTextSize(sp);
        // 获得输入字的宽高
        Rect bounds = new Rect();
        String text=getNewText(nickname);

        paint.getTextBounds(text, 0,text.length(), bounds);
        float x, y;
        x = 40 - bounds.width() / 2;
        y = 40 + bounds.height() / 2;
        canvas.drawText(text, x, y, paint);
        return bitmap;
    }
    /**
     * 返回处理过的昵称,保证显示两个或两个一下,包含中文默认取后两个,非中文取前两个
     * */
    private static String getNewText(String content){
        String text;
        if(isChinese(content)){
            if(content.length()>2){
                text=content.substring(content.length()-2,content.length());
            }
            else{
                text=content;
            }
        }
        else{
            if(content.length()>2){
                text=content.substring(0,2);
            }
            else{
                text=content;
            }
        }
        return text;
    }
    // 判断一个字符是否是中文
    public static boolean isChinese(char c) {
        return c >= 0x4E00 &&  c <= 0x9FA5;// 根据字节码判断
    }
    // 判断一个字符串是否含有中文
    public static boolean isChinese(String str) {
        if (str == null) return false;
        for (char c : str.toCharArray()) {
            if (isChinese(c)) return true;// 有一个中文字符就返回
        }
        return false;
    }


    // 将bitmap转成string类型通过Base64
    public static String BitmapToString(Bitmap bitmap) {
        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //可以设计添加头
        //String title=""data:image/jpeg;base64,";
        return title+result;

    }
 /**
     *  
     *  * base64转为bitmap 
     *  * @param base64Data 
     *  * @return 
     *  
     */
    public static Bitmap base64ToBitmap(String base64Data) {
            byte[] bytes = Base64.decode(base64Data.replaceFirst("data:image/jpeg;base64,", "").trim(), Base64.DEFAULT);
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }
}

通过上述的工具类,就可以将文字传入,并且获得圆形的Bitmap,剩下的可以根据自己的业务逻辑去设计

bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);

这里必须使用PNG,否则的话会出现黑边

BUG:将base64转化为bitmap后清晰度上回有所下降,暂时没有去解决。

猜你喜欢

转载自blog.csdn.net/qq_24536171/article/details/66970793
今日推荐