Android学习札记48:将TextView中的指定文字转换为表情显示

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










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值