Text---View部- 分-文- 字-高-亮-显-示-并-同-时-显-示-表-情




package com.timgor.util;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.timgor.flymebo.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.widget.TextView;


/**
 * 处理TextView界面的类
 * @author wyt
 * @version 2012-09-23
 *
 */
public class TextAutoLink {
	
	private Context mContext;
	//private String[] mSmileyTexts;
	private Pattern mPattern;
	private HashMap<String, Integer> mSmileyToRes;
	
	public TextAutoLink(Context context) {
		mContext = context;
		mSmileyToRes = buildSmileyToRes();
		mPattern = buildPattern();
	}

	// 加入话题 好友 URL的连结
	public char strarray[];

	public void addURLSpan(TextView textView, String str, int motionWidth, int motionHeight) {
		try{
			str = str + " ";
			SpannableString ss = new SpannableString(str);
			strarray = str.toCharArray();
			int l = str.length() - 10;
			for (int i = 0; i < l; i++) {
				if (strarray[i] == 'h' && strarray[i + 1] == 't'
						&& strarray[i + 2] == 't' && strarray[i + 3] == 'p'
						&& strarray[i + 4] == ':' && strarray[i + 5] == '/'
						&& strarray[i + 6] == '/') {
					if (strarray.length > i + 8 && strarray[i + 7] == 't' && strarray[i + 8] == '.') {
						StringBuffer sb = new StringBuffer("http://t.");
						for (int j = i + 9; strarray.length > j && j < i+20; j++) {
							sb.append(strarray[j]);
						}
						Log.d("http", sb.toString());
						ss.setSpan(new URLSpan(sb.toString()), i, i+19,
								Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						i = i+19;
					}else{
						StringBuffer sb = new StringBuffer("http://");
						for (int j = i + 7; true; j++) {
							if (strarray[j] != ' ')
								sb.append(strarray[j]);
							else {
								Log.d("http", sb.toString());
								ss.setSpan(new URLSpan(sb.toString()), i, j,
										Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
								i = j;
								break;
							}
						}
					}
					
				}
			}
			// 处理话题
			l = str.length();
			StringBuffer sb = null;
			boolean start = false;
			int startIndex = 0;
			for (int i = 0; i < l; i++) {
				if (strarray[i] == '@') {
					start = true;
					sb = new StringBuffer("weibo://weibo.view/");
					startIndex = i;
				} else {
					if (start) {
						if (strarray[i] == ':' || strarray[i] == ' ') {
							ss.setSpan(new URLSpan(sb.toString()), startIndex, i,
									Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
							sb = null;
							start = false;
						} else {
							sb.append(strarray[i]);
						}
					}
				}

			}
			// 处理 话题
			start = false;
			startIndex = 0;
			for (int i = 0; i < l; i++) {
				if (strarray[i] == '#') {
					if (!start) {
						start = true;
						sb = new StringBuffer("weibo://weibo.view/");
						startIndex = i;
					} else {
						sb.append('#');
						ss.setSpan(new URLSpan(sb.toString()), startIndex, i + 1,
								Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						sb = null;
						start = false;
					}
				} else {
					if (start) {
						sb.append(strarray[i]);
					}
				}
			}
			
                        //处理显示表情
			String content = str;
//			int len = 0;
//			int starts = 0;
//			int end = 0;
			
			Matcher matcher = mPattern.matcher(content);
			while (matcher.find()) {
				int resId = mSmileyToRes.get(matcher.group());
				//得到要显示图片的资源
				Drawable drawable = mContext.getResources().getDrawable(resId);
				//设置高度
				if (drawable != null) {
					drawable.setBounds(0, 0, motionWidth, /*drawable.getIntrinsicHeight()*/motionHeight); 
			        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);  
			        ss.setSpan(span, matcher.start(),
							matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
				}
			}
			
//			while(len < content.length()){
//				if(content.indexOf("[", starts) != -1 && content.indexOf("]", end) != -1){
//					starts = content.indexOf("[", starts);
//					end = content.indexOf("]", end);
//					String phrase = content.substring(starts,end + 1);
//					int id = -1;
//					for (int i = 0; i < EmotionStrings.length; i++) {
//						if (phrase.equals(EmotionStrings[i])) {
//							id = EmotionsParser.Emotions[i];
//							break;
//						}
//					}
//					
//					if (id != -1) {
//						try {
//							Drawable drawable = mContext.getResources().getDrawable(id);//.getactivity.getResources().getDrawable(i);  
//							if (drawable != null) {
//								drawable.setBounds(0, 0, motionWidth, /*drawable.getIntrinsicHeight()*/motionHeight); 
//						        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);  
//						        ss.setSpan(span, starts,end + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);  
//							}
//						} catch (SecurityException e) {
//							e.printStackTrace();
//						} catch (IllegalArgumentException e) {
//							e.printStackTrace();
//						}
//					}
//					
//					starts = end;
//					len = end;
//					end++;
//				}else{
//					starts++;
//					end++;
//					len = end;
//				}
//			}
			
			textView.setText(ss); // 设定TextView话题和url和好友 连接
			strarray = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private HashMap<String, Integer> buildSmileyToRes() {
		if (Emotions.length != EmotionStrings.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>(
				EmotionStrings.length);
		for (int i = 0; i < EmotionStrings.length; i++) {
			smileyToRes.put(EmotionStrings[i], Emotions[i]);
		}

		return smileyToRes;
	}

	// 构建正则表达式
	private Pattern buildPattern() {
		StringBuilder patternString = new StringBuilder(EmotionStrings.length * 3);
		patternString.append('(');
		for (String s : EmotionStrings) {
			patternString.append(Pattern.quote(s));
			patternString.append('|');
		}
		patternString.replace(patternString.length() - 1, patternString
				.length(), ")");

		return Pattern.compile(patternString.toString());
	}
	
	public static final String EmotionStrings[] = { "[din推撞]", "[bed凌乱]", "[din厉害]", "[呵呵]", "[嘻嘻]", "[哈哈]",
		"[爱你]", "[晕]", "[泪]", "[馋嘴]", "[抓狂]", "[哼]", /* "[抱抱]", */"[可爱]",
		"[怒]", "[汗]", "[困]", "[害羞]", "[睡觉]", "[钱]", "[偷笑]", "[酷]", "[衰]",
		"[吃惊]", "[闭嘴]", "[鄙视]", "[挖鼻屎]", "[花心]", "[鼓掌]", "[失望]", "[思考]",
		"[生病]", "[亲亲]", "[怒骂]", "[太开心]", "[懒得理你]", "[右哼哼]", "[左哼哼]", "[嘘]",
		"[委屈]", "[吐]", "[可怜]", "[打哈气]", "[顶]", "[疑问]", "[握手]", "[耶]",
		"[good]", "[弱]", "[不要]", "[ok]", "[赞]", "[来]", "[蛋糕]", "[心]",
		"[伤心]", "[钟]", "[猪头]", "[话筒]", "[月亮]", "[下雨]", "[太阳]", "[蜡烛]",
		"[风扇]", "[男孩儿]", "[女孩儿]", "[围观]", "[威武]", "[给力]", "[神马]", "[浮云]",
		"[奥特曼]", "[兔子]", "[熊猫]", "[飞机]", "[orz]", "[调戏]", "[乐乐]", "[欢欢]",
		"[囧]"
/*
 * "[失望]", "[挖鼻屎]", "[黑线]", "[花心]", "[可爱]", "[吐]", "[委屈]", "[思考]", "[哈哈]",
 * "[嘘]", "[右哼哼]", "[左哼哼]", "[疑问]", "[阴险]", "[爱你]", "[馋嘴]", "[顶]", "[钱]",
 * "[嘻嘻]", "[汗]", "[呵呵]", "[睡觉]", "[困]", "[害羞]", "[悲伤]", "[鄙视]", "[抱抱]",
 * "[拜拜]", "[怒]", "[吃惊]", "[闭嘴]", "[泪]", "[偷笑]", "[哼]", "[晕]", "[衰]",
 * "[抓狂]", "[愤怒]", "[感冒]", "[鼓掌]", "[酷]", "[来]", "[good]", "[haha]", "[不要]",
 * "[ok]", "[拳头]", "[弱]", "[握手]", "[赞]", "[耶]", "[最差]", "[右抱抱]", "[左抱抱]",
 * "[粉红丝带]", "[心]", "[围脖]", "[温暖帽子]", "[手套]", "[红包]", "[喜]", "[礼物]", "[蛋糕]",
 * "[钻戒]", "[钻石]", "[大巴]", "[飞机]", "[自行车]", "[汽车]", "[手机]", "[照相机]", "[药]",
 * "[电脑]", "[手纸]", "[落叶]", "[圣诞树]", "[圣诞帽]", "[圣诞老人]", "[圣诞铃铛]", "[圣诞袜]",
 * "[康乃馨]", "[图片]", "[六芒星]", "[地球一小时]", "[植树节]", "[粉蛋糕]", "[糖果]", "[万圣节]",
 * "[火炬]", "[酒壶]", "[月饼]", "[满月]", "[巧克力]", "[脚印]", "[酒]", "[狗]", "[工作]",
 * "[档案]", "[叶子]", "[钢琴]", "[印迹]", "[钟]", "[茶]", "[西瓜]", "[雨伞]", "[电视机]",
 * "[电话]", "[太阳]", "[星]", "[哨子]", "[话筒]", "[音乐]", "[电影]", "[月亮]", "[唱歌]",
 * "[冰棍]", "[房子]", "[帽子]", "[足球]", "[鲜花]", "[花]", "[风扇]", "[干杯]", "[咖啡]",
 * "[兔子]", "[神马]", "[浮云]", "[给力]", "[萌]", "[鸭梨]", "[熊猫]", "[互粉]", "[织]",
 * "[围观]", "[扔鸡蛋]", "[奥特曼]", "[威武]", "[伤心]", "[热吻]", "[囧]", "[orz]", "[宅]",
 * "[小丑]", "[帅]", "[猪头]", "[实习]", "[骷髅]", "[便便]", "[雪人]", "[黄牌]", "[红牌]",
 * "[跳舞花]", "[礼花]", "[打针]", "[叹号]", "[问号]", "[句号]", "[逗号]", "[闪]", "[啦啦]",
 * "[吼吼]", "[庆祝]", "[嘿]"
 */};
public static final int Emotions[] = { R.drawable.dintuizhuang_thumb, R.drawable.brdlingluan_thumb, R.drawable.dinlihai_thumb,  R.drawable.smile, R.drawable.tooth,
		R.drawable.laugh, R.drawable.love, R.drawable.dizzy,
		R.drawable.sad, R.drawable.cz_org, R.drawable.crazy,
		R.drawable.hate, R.drawable.tz_org, R.drawable.angry,
		R.drawable.sweat, R.drawable.sleepy, R.drawable.shame_org,
		R.drawable.sleep_org, R.drawable.money_org, R.drawable.hei_org,
		R.drawable.cool_org, R.drawable.cry, R.drawable.cj_org,
		R.drawable.bz_org, R.drawable.bs2_org, R.drawable.kbs_org,
		R.drawable.hs_org, R.drawable.gz_org, R.drawable.sw_org,
		R.drawable.sk_org, R.drawable.sb_org, R.drawable.qq_org,
		R.drawable.nm_org, R.drawable.mb_org, R.drawable.ldln_org,
		R.drawable.yhh_org, R.drawable.zhh_org, R.drawable.x_org,
		R.drawable.wq_org, R.drawable.t_org, R.drawable.kl_org,
		R.drawable.k_org, R.drawable.d_org, R.drawable.yw_org,
		R.drawable.ws_org, R.drawable.ye_org, R.drawable.good_org,
		R.drawable.sad_org, R.drawable.no_org, R.drawable.ok_org,
		R.drawable.z2_org, R.drawable.come_org, R.drawable.cake,
		R.drawable.heart, R.drawable.unheart, R.drawable.clock_org,
		R.drawable.pig, R.drawable.m_org, R.drawable.moon, R.drawable.rain,
		R.drawable.sun, R.drawable.lazu_org, R.drawable.fan,
		R.drawable.kissgirl_org, R.drawable.kissboy_org, R.drawable.wg_org,
		R.drawable.vw_org, R.drawable.geili_org, R.drawable.horse2_org,
		R.drawable.fuyun_org, R.drawable.otm_org, R.drawable.rabbit_org,
		R.drawable.panda_org, R.drawable.travel_org, R.drawable.orz1_thumb,
		R.drawable.tiaoxi_thumb, R.drawable.guanbuzhao_thumb,
		R.drawable.liaobuqi_thumb, R.drawable.j_thumb };
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值