import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SmileyParser {
private Context mContext;
private String[] mSmileyTexts;
private HashMap<String, Integer> mSmileyToRes;
public static final int DEFAULT_SMILEY_TEXTS = R.array.default_smiley_texts;
public static final int[] DEFAULT_SMILEY_RES_IDS = {
R.drawable.aini,
R.drawable.aoteman,
R.drawable.baibai,
R.drawable.baobao,
R.drawable.beiju,
R.drawable.beishang,
R.drawable.bianbian,
R.drawable.bishi,
R.drawable.bizui,
R.drawable.buyao,
R.drawable.chanzui
};
private Pattern mPattern;
public SmileyParser(Context context) {
mContext = context;
mSmileyTexts = mContext.getResources().getStringArray(DEFAULT_SMILEY_TEXTS);
mSmileyToRes = buildSmileyToRes();
mPattern = buildPattern();
}
private HashMap<String, Integer> buildSmileyToRes() {
if (DEFAULT_SMILEY_RES_IDS.length != mSmileyTexts.length) {
// Log.w("SmileyParser", "Smiley resource ID/text mismatch");
// 表情的数量需要和数组定义的长度一致!
throw new IllegalStateException("Smiley resource ID/text mismatch");
}
HashMap<String, Integer> smileyToRes = new HashMap<String, Integer>(mSmileyTexts.length);
for (int i = 0; i < mSmileyTexts.length; i++) {
smileyToRes.put(mSmileyTexts[i], DEFAULT_SMILEY_RES_IDS[i]);
}
return smileyToRes;
}
// 构建正则表达式
private Pattern buildPattern() {
StringBuilder patternString = new StringBuilder(mSmileyTexts.length * 3);
patternString.append('(');
for (String s : mSmileyTexts) {
patternString.append(Pattern.quote(s));
patternString.append('|');
}
patternString.replace(patternString.length() - 1, patternString.length(), ")");
return Pattern.compile(patternString.toString());
}
// 根据文本替换成图片
public CharSequence replace(CharSequence text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Matcher matcher = mPattern.matcher(text);
while (matcher.find()) {
int resId = mSmileyToRes.get(matcher.group());
builder.setSpan(new ImageSpan(mContext, resId),matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}
}
在 res/arrays.xml 中添加数组:
<?xml version="1.0" encoding="UTF-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"、
>
<string-array name="default_smiley_texts">
<item >[爱你]</item>
<item >[机器人]</item>
<item >[拜拜]</item>
<item >[抱抱]</item>
<item >[悲剧]</item>
<item >[悲伤]</item>
<item >[便便]</item>
<item >[鄙视]</item>
<item >[闭嘴]</item>
<item >[不要]</item>
<item >[馋嘴]</item>
</string-array>
</resources>
使用:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvShow = (TextView) findViewById(R.id.tvShow);
TextView tvSource = (TextView) findViewById(R.id.tvSource);
String text = "[鄙视][爱你][便便][馋嘴][机器人]";
tvSource.setText("原文:"+text);
SmileyParser parser = new SmileyParser(this);
tvShow.setText(parser.replace("SmileyParser类处理后:"+text));
}
转载自:
http://gundumw100.iteye.com/blog/904107