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"?><resourcesxmlns: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










更多相关文章

  1. Activity之间传递类对象
  2. Android(安卓)build.gradle buildConfigField 配置数组
  3. Android开发便签9:在android资源文件中定义字符串数组
  4. Android上传图片到服务器,android-async-http上传图片到服务器
  5. Android(安卓)如何判断CPU是32位还是64位
  6. android Java BASE64编码和解码二:图片的编码和解码
  7. Android之dialog
  8. Android期末项目(一)—— 解析二维数组对象
  9. android 数据传输之JSON

随机推荐

  1. 转载 android 利用ksoap2方式连接webserv
  2. android UI小结(二)
  3. 初探Android
  4. 【Android(安卓)系统开发】 编译 Android
  5. android开发初学建议
  6. Android(安卓)开发小技巧
  7. Android高斯模糊、高斯平滑(Gaussian Blur
  8. 如何发布你的Android应用程序
  9. Android如何让TextView子元素随着父元素
  10. android studio导入quick cocos2dxV3.3的