public class FaceUtils { private static HashMap<String, Bitmap>faceMap = null; private static void initMaps(Context context) { faceMap = new HashMap<String, Bitmap>(); String[] faces_res_name = context.getResources().getStringArray(R.array.faces_res_name); String[] faces_alias = context.getResources().getStringArray(R.array.faces_alias); for(int i=0;i<faces_res_name.length;i++) { int id = context.getResources().getIdentifier(faces_res_name[i], "drawable", context.getPackageName()); Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id); int w = context.getResources().getDimensionPixelOffset(R.dimen.face_img_w_big); bmp = Bitmap.createScaledBitmap(bmp, w, w, true); faceMap.put(faces_alias[i], bmp); } } public static SpannableStringBuilder decodeFaceInText(Context context, String message) { if (null == faceMap) { initMaps(context); } SpannableStringBuilder ssb = new SpannableStringBuilder(); ssb.append(message); Pattern pattern = Pattern.compile("\\[\\w+\\]"); Matcher matcher = pattern.matcher(message); int index = 0, length = 0; while (matcher.find()) { String faceFind = matcher.group(); index = message.indexOf(faceFind,index); length = faceFind.length(); Bitmap map = faceMap.get(faceFind); if (map != null) { ssb.setSpan(new ImageSpan(context,map) , index , index + length , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } index = index + length; } return ssb; } /** * 翻译文字中的表情 */ private static boolean test = true; public static SpannableStringBuilder translateFaceInText(Context context, String message) { if (test) { message = "[aixin]fasafdsafdsafdsafdsafdsafdsafs[bishi]fasafdsafdsafdsafdsafdsafdsafs[daku]fasafdsafdsafdsafdsafdsafdsafs[daxiao]fasafdsafdsafdsafdsafdsafdsafs[tiaopi]fasafdsafdsafdsafdsafdsafdsafs[haixiu]fasafdsafdsafdsafdsafdsafdsafs[han]fasafdsafdsafdsafdsafdsafdsafs[huaixiao]fasafdsafdsafdsafdsafdsafdsafs[jinya]fasafdsafdsafdsafdsafdsafdsafs[jiong]fasafdsafdsafdsafdsafdsafdsafs[nanguo]fasafdsafdsafdsafdsafdsafdsafs[outu]fasafdsafdsafdsafdsafdsafdsafs[qinqin]fasafdsafdsafdsafdsafdsafdsafs[shenqi]fasafdsafdsafdsafdsafdsafdsafs[shuijiao]fasafdsafdsafdsafdsafdsafdsafs[weixiao][weiqu][yiwen][yun][zaijian]hello[aixin]hello[bishi]hello[daku]hello[daxiao]hello[tiaopi]hello[haixiu]hello[han]hello[huaixiao]hello[jinya]hello[jiong]hello[nanguo]hello[outu]hello[qinqin]hello[shenqi]hello[shuijiao]hello[weixiao]hello[weiqu]hello[yiwen]hello[yun]hello[zaijian]hello[aixin]hello[bishi]hello[daku]hello[daxiao]hello[tiaopi]hello[haixiu]hello[han]hello[huaixiao]hello[jinya]hello[jiong]hello[nanguo]hello[outu]hello[qinqin]hello[shenqi]hello[shuijiao]hello[weixiao]hello[weiqu]hello[yiwen]hello[yun]hello[zaijian][aixin][bishi][daku][daxiao][tiaopi][haixiu][han][huaixiao][jinya][jiong][nanguo][outu][qinqin][shenqi][shuijiao][weixiao][weiqu][yiwen][yun][zaijian][aixin][bishi][daku][daxiao][tiaopi][haixiu][han][huaixiao][jinya][jiong][nanguo][outu][qinqin][shenqi][shuijiao][weixiao][weiqu][yiwen][yun][zaijian][aixin][bishi][daku][daxiao][tiaopi][haixiu][han][huaixiao][jinya][jiong][nanguo][outu][qinqin][shenqi][shuijiao][weixiao][weiqu][yiwen][yun][zaijian][aixin][bishi][daku][daxiao][tiaopi][haixiu]1234234535435324242[han]1234234535435324242[huaixiao]1234234535435324242[jinya]1234234535435324242[jiong]1234234535435324242[nanguo]1234234535435324242[outu]1234234535435324242[qinqin]1234234535435324242[shenqi]1234234535435324242[shuijiao]1234234535435324242[weixiao]1234234535435324242[weiqu]1234234535435324242[yiwen]1234234535435324242[yun]1234234535435324242[zaijian]1234234535435324242[aixin]1234234535435324242[bishi]1234234535435324242[daku][daxiao]1234234535435324242[tiaopi]1234234535435324242[haixiu]1234234535435324242[han]1234234535435324242[huaixiao]1234234535435324242[jinya]1234234535435324242[jiong]1234234535435324242[nanguo]1234234535435324242[outu]1234234535435324242[qinqin]1234234535435324242[shenqi]1234234535435324242[shuijiao]1234234535435324242[weixiao]1234234535435324242[weiqu]1234234535435324242[yiwen]1234234535435324242[yun]1234234535435324242[zaijian]123[4234]535435324242[aixin]123[4234]535435324242[bishi]123[4234]535435324242[daku]123[4234]535435324242[daxiao]123[4234]535435324242[tiaopi]123[4234]535435324242[haixiu]123[4234]535435324242[han]123[4234]535435324242[huaixiao]123[4234]535435324242[jinya]123[4234]535435324242[jiong]123[4234]535435324242[nanguo]123[4234]535435324242[outu]123[4234]535435324242[qinqin]123[4234]535435324242[shenqi]123[4234]535435324242[shuijiao]123[4234]535435324242[weixiao]123[4234]535435324242[weiqu]123[4234]535435324242[yiwen]123[4234]535435324242[yun]123[4234]535435324242[zaijian]123[4234]535435324242[aixin]123[4234]535435324242[bishi]123[4234]535435324242[daku]123[4234]535435324242[daxiao]123[4234]535435324242[tiaopi]123[4234]535435324242[haixiu]123[4234]535435324242[han]123[4234]535435324242[huaixiao]1234234535435324242[jinya]1234234535435324242[jiong]1234234535435324242[nanguo]1234234535435324242[outu]1234234535435324242[qinqin]1234234535435324242[shenqi]1234234535435324242[shuijiao]1234234535435324242[weixiao]1234234535435324242[weiqu]1234234535435324242[yiwen]1234234535435324242[yun]1234234535435324242[zaijian]1234234535435324242[aixin]1234234535435324242[bishi]1234234535435324242[daku]1234234535435324242[daxiao]1234234535435324242[tiaopi]1234234535435324242[haixiu]1234234535435324242[han]1234234535435324242[huaixiao]1234234535435324242[jinya]1234234535435324242[jiong]1234234535435324242[nanguo]1234234535435324242[outu]1234234535435324242[qinqin]1234234535435324242[shenqi]1234234535435324242[shuijiao]1234234535435324242[weixiao]1234234535435324242[weiqu]1234234535435324242[yiwen]1234234535435324242[yun]1234234535435324242[zaijian]1234234535435324242[aixin]12334535435324242[bishi]1234234535435324242[daku]1234234535435324242[daxiao]1234234535435324242[tiaopi]1234234535435324242[haixiu]1234234535435324242[han]1234234535435324242[huaixiao]1234234535435324242[jinya]1234234535435324242[jiong]1234234535435324242[nanguo]1234234535435324242[outu]1234234535435324242[qinqin]1234234535435324242[shenqi]1234234535435324242[shuijiao]1234234535435324242[weixiao]1234234535435324242[weiqu]1234234535435324242[yiwen]1234234535435324242[yun]1234234535435324242[zaijian]1234234535435324242[aixin]1234234535435324242[bishi]1234234535435324242[daku]1234234535435324242[daxiao]1234234535435324242[tiaopi]1234234535435324242[haixiu]1234234535435324242[han]1234234535435324242[huaixiao]1234234535435324242[jinya]1234234535435324242[jiong]1234234535435324242[nanguo]1234234535435324242[outu]1234234535435324242[qinqin]1234234535435324242[shenqi]1234234535435324242[shuijiao]1234234535435324242[weixiao]1234234535435324242[weiqu]1234234535435324242[yiwen]1234234535435324242[yun]1234234535435324242[zaijian][aixin]1234234535435324242[bishi]1234234535435324242[daku]1234234535435324242[daxiao]1234234535435324242[tiaopi]1234234535435324242[haixiu]1234234535435324242[han]1234234535435324242[huaixiao]1234234535435324242[jinya]1234234535435324242[jiong]1234234535435324242[nanguo]1234234535435324242[outu]1234234535435324242[qinqin]1234234535435324242[shenqi]1234234535435324242[shuijiao]1234234535435324242[weixiao]1234234535435324242[weiqu][yiwen][yun][zaijian][aixin][bishi][daku][daxiao][tiaopi][haixiu][han][huaixiao][jinya][jiong][nanguo][outu][qinqin][shenqi][shuijiao][weixiao][weiqu][yiwen][yun][zaijian]"; } return decodeFaceInText(context, message); } }
发表评论