EmojiUtils
Posted 杨伟乔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EmojiUtils相关的知识,希望对你有一定的参考价值。
package com.fone.livechannel.ui.room;
import java.io.FileInputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.WindowManager;
import com.fone.livechannel.AppAplication;
import com.fone.livechannel.R;
import com.fone.livechannel.ui.room.websocket.WSConstant;
import com.fone.livechannel.utils.L;
public class EmojiUtils {
private static String TAG = "EmojiUtils";
public EmojiUtils() {
}
public static SpannableString getEmojiString(Context context, String msg,
String regex, String bySpeaker) {
SpannableString spannableString = new SpannableString(msg);
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); // 通过传入的正则表达式来生成一个pattern
try {
dealEmoji(context, spannableString, pattern, 0, bySpeaker, msg);
} catch (Exception e) {
L.e(TAG, TAG, e.getMessage());
}
return spannableString;
}
private static void dealEmoji(Context context,
SpannableString spannableString, Pattern pattern, int start, String bySpeaker, String msg)
throws Exception {
String code = null;
Matcher matcher = pattern.matcher(spannableString);
while (matcher.find()) {
String key = matcher.group();
Bitmap bm = null;
if (matcher.start() < start) {
continue;
}
if (key != null && key.contains(WSConstant.ADD_ICON)) {
code = key.substring(0, key.lastIndexOf(WSConstant.ADD_ICON));
}
if (code.startsWith("B_1")) {
bm = BitmapFactory.decodeResource(context.getResources(),
R.drawable.chat_gift_free_acquire);
} else if (code.startsWith("B_2")) {
bm = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_vip);
} else {
bm = BitmapFactory.decodeFile(RoomUtil.IMAGE_BASE_PATH + code);
}
if (bm == null) {
FileInputStream inStream = context.openFileInput(code);// 只需传文件名
bm = BitmapFactory.decodeStream(inStream);
}
if (bm == null) {
break;
}
int size;
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
int screenWidth = wm.getDefaultDisplay().getWidth();
if (code.startsWith("Q_")) {
size = screenWidth / 20;
} else if (code.startsWith("G_")) {
size = screenWidth / 12;
} else if (code.startsWith("B")) {
size = screenWidth / 22;
} else {
size = bm.getWidth() * 2;
}
Drawable drawable = new BitmapDrawable(bm);
drawable.setBounds(0, 0, size, size);
ImageSpan imageSpan = null;
if (code.startsWith("B_2")) {
imageSpan = new ImageSpan(drawable,
DynamicDrawableSpan.ALIGN_BASELINE); // ?通过图片资源id来得到bitmap,用一个ImageSpan来包装
} else {
imageSpan = new ImageSpan(drawable,
DynamicDrawableSpan.ALIGN_BOTTOM); // ?通过图片资源id来得到bitmap,用一个ImageSpan来包装
}
int end = matcher.start() + key.length(); // 计算该图片名字的长度,也就是要替换的字符串的长度
spannableString.setSpan(imageSpan, matcher.start(), end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 将该图片替换字符串中规定的位置中
int bySpeakerStart = msg.indexOf(bySpeaker);
int bySpeakerEnd = bySpeakerStart + bySpeaker.length();
int colorid = AppAplication.getGlobalContext().getResources().getColor(R.color.room_tab_text_click_color);
if (bySpeakerEnd != 0){
spannableString.setSpan(new ForegroundColorSpan(colorid), bySpeakerStart, bySpeakerEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (end < spannableString.length()) { // 如果整个字符串还未验证完,则继续
dealEmoji(context, spannableString, pattern, end, bySpeaker, msg);
}
break;
}
}
}
以上是关于EmojiUtils的主要内容,如果未能解决你的问题,请参考以下文章