EmojiUtils

Posted 杨伟乔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EmojiUtils相关的知识,希望对你有一定的参考价值。

  1. package com.fone.livechannel.ui.room;
  2. import java.io.FileInputStream;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Color;
  9. import android.graphics.drawable.BitmapDrawable;
  10. import android.graphics.drawable.Drawable;
  11. import android.text.SpannableString;
  12. import android.text.Spanned;
  13. import android.text.style.DynamicDrawableSpan;
  14. import android.text.style.ForegroundColorSpan;
  15. import android.text.style.ImageSpan;
  16. import android.view.WindowManager;
  17. import com.fone.livechannel.AppAplication;
  18. import com.fone.livechannel.R;
  19. import com.fone.livechannel.ui.room.websocket.WSConstant;
  20. import com.fone.livechannel.utils.L;
  21. public class EmojiUtils {
  22. private static String TAG = "EmojiUtils";
  23. public EmojiUtils() {
  24. }
  25. public static SpannableString getEmojiString(Context context, String msg,
  26. String regex, String bySpeaker) {
  27. SpannableString spannableString = new SpannableString(msg);
  28. Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); // 通过传入的正则表达式来生成一个pattern
  29. try {
  30. dealEmoji(context, spannableString, pattern, 0, bySpeaker, msg);
  31. } catch (Exception e) {
  32. L.e(TAG, TAG, e.getMessage());
  33. }
  34. return spannableString;
  35. }
  36. private static void dealEmoji(Context context,
  37. SpannableString spannableString, Pattern pattern, int start, String bySpeaker, String msg)
  38. throws Exception {
  39. String code = null;
  40. Matcher matcher = pattern.matcher(spannableString);
  41. while (matcher.find()) {
  42. String key = matcher.group();
  43. Bitmap bm = null;
  44. if (matcher.start() < start) {
  45. continue;
  46. }
  47. if (key != null && key.contains(WSConstant.ADD_ICON)) {
  48. code = key.substring(0, key.lastIndexOf(WSConstant.ADD_ICON));
  49. }
  50. if (code.startsWith("B_1")) {
  51. bm = BitmapFactory.decodeResource(context.getResources(),
  52. R.drawable.chat_gift_free_acquire);
  53. } else if (code.startsWith("B_2")) {
  54. bm = BitmapFactory.decodeResource(context.getResources(),
  55. R.drawable.icon_vip);
  56. } else {
  57. bm = BitmapFactory.decodeFile(RoomUtil.IMAGE_BASE_PATH + code);
  58. }
  59. if (bm == null) {
  60. FileInputStream inStream = context.openFileInput(code);// 只需传文件名
  61. bm = BitmapFactory.decodeStream(inStream);
  62. }
  63. if (bm == null) {
  64. break;
  65. }
  66. int size;
  67. WindowManager wm = (WindowManager) context
  68. .getSystemService(Context.WINDOW_SERVICE);
  69. int screenWidth = wm.getDefaultDisplay().getWidth();
  70. if (code.startsWith("Q_")) {
  71. size = screenWidth / 20;
  72. } else if (code.startsWith("G_")) {
  73. size = screenWidth / 12;
  74. } else if (code.startsWith("B")) {
  75. size = screenWidth / 22;
  76. } else {
  77. size = bm.getWidth() * 2;
  78. }
  79. Drawable drawable = new BitmapDrawable(bm);
  80. drawable.setBounds(0, 0, size, size);
  81. ImageSpan imageSpan = null;
  82. if (code.startsWith("B_2")) {
  83. imageSpan = new ImageSpan(drawable,
  84. DynamicDrawableSpan.ALIGN_BASELINE); // ?通过图片资源id来得到bitmap,用一个ImageSpan来包装
  85. } else {
  86. imageSpan = new ImageSpan(drawable,
  87. DynamicDrawableSpan.ALIGN_BOTTOM); // ?通过图片资源id来得到bitmap,用一个ImageSpan来包装
  88. }
  89. int end = matcher.start() + key.length(); // 计算该图片名字的长度,也就是要替换的字符串的长度
  90. spannableString.setSpan(imageSpan, matcher.start(), end,
  91. Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 将该图片替换字符串中规定的位置中
  92. int bySpeakerStart = msg.indexOf(bySpeaker);
  93. int bySpeakerEnd = bySpeakerStart + bySpeaker.length();
  94. int colorid = AppAplication.getGlobalContext().getResources().getColor(R.color.room_tab_text_click_color);
  95. if (bySpeakerEnd != 0){
  96. spannableString.setSpan(new ForegroundColorSpan(colorid), bySpeakerStart, bySpeakerEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  97. }
  98. if (end < spannableString.length()) { // 如果整个字符串还未验证完,则继续
  99. dealEmoji(context, spannableString, pattern, end, bySpeaker, msg);
  100. }
  101. break;
  102. }
  103. }
  104. }





以上是关于EmojiUtils的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数