EditText通常用于显示文字,但有时候也需要在文字中夹杂一些图片,比如QQ中就可以使用表情图片,又比如需要的文字高亮显示等等,如何在android中也做到这样呢?
记得android中有个android.text包,这里提供了对文本的强大的处理功能。
添加图片主要用SpannableString和ImageSpan类:
Drawable drawable = getResources().getDrawable(id); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); //需要处理的文本,[smile]是需要被替代的文本 SpannableString spannable = new SpannableString(getText().toString()+"[smile]"); //要让图片替代指定的文字就要用ImageSpan ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE); //开始替换,注意第2和第3个参数表示从哪里开始替换到哪里替换结束(start和end) //最后一个参数类似数学中的集合,[5,12)表示从5到12,包括5但不包括12 spannable.setSpan(span, getText().length(),getText().length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); setText(spannable);
public void underline(int start,int end){ SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString()); CharacterStyle span=new UnderlineSpan(); spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); setText(spannable); }
SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString()); CharacterStyle span_1=new StyleSpan(android.graphics.Typeface.ITALIC); CharacterStyle span_2=new ForegroundColorSpan(Color.RED); spannable.setSpan(span_1, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannable.setSpan(span_2, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); setText(spannable);
/** * 带有\n换行符的字符串都可以用此方法显示2种颜色 * @param text * @param color1 * @param color2 * @return */ public SpannableStringBuilder highlight(String text,int color1,int color2,int fontSize){ SpannableStringBuilder spannable=new SpannableStringBuilder(text);//用于可变字符串 CharacterStyle span_0=null,span_1=null,span_2; int end=text.indexOf("\n"); if(end==-1){//如果没有换行符就使用第一种颜色显示 span_0=new ForegroundColorSpan(color1); spannable.setSpan(span_0, 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }else{ span_0=new ForegroundColorSpan(color1); span_1=new ForegroundColorSpan(color2); spannable.setSpan(span_0, 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannable.setSpan(span_1, end+1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); span_2=new AbsoluteSizeSpan(fontSize);//字体大小 spannable.setSpan(span_2, end+1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return spannable; }
/** * 关键字高亮显示 * @param target 需要高亮的关键字 */ public void highlight(String target){ String temp=getText().toString(); SpannableStringBuilder spannable = new SpannableStringBuilder(temp); CharacterStyle span=null; Pattern p = Pattern.compile(target); Matcher m = p.matcher(temp); while (m.find()) { span = new ForegroundColorSpan(Color.RED);//需要重复! spannable.setSpan(span, m.start(), m.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } setText(spannable); }循环太慢,不知道有没有简单高效的方法?
下面给出一个类,专门用于将指定的文字转换成表情显示,当然使用前表情图片需要先导入drawable,另外表情图片的数量需要和数组定义的长度一致!
package hfut.gmm; import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; import android.content.Context; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.style.ImageSpan; import com.weibo.android.R; public class SmileyParser { private static final int[]DEFAULT_SMILEY_RES_IDS ={ R.drawable.f0,R.drawable.f1,R.drawable.f2,R.drawable.f3,R.drawable.f4,R.drawable.f5,R.drawable.f6,R.drawable.f7,R.drawable.f8, R.drawable.f9,R.drawable.f10,R.drawable.f11,R.drawable.f12,R.drawable.f13,R.drawable.f14,R.drawable.f15,R.drawable.f16,R.drawable.f17, R.drawable.f18,R.drawable.f19,R.drawable.f20,R.drawable.f21,R.drawable.f22,R.drawable.f23,R.drawable.f24,R.drawable.f25,R.drawable.f26, R.drawable.f27,R.drawable.f28,R.drawable.f29,R.drawable.f30,R.drawable.f31,R.drawable.f32,R.drawable.f33,R.drawable.f34,R.drawable.f35, R.drawable.f36,R.drawable.f37,R.drawable.f38,R.drawable.f39,R.drawable.f40,R.drawable.f41,R.drawable.f42,R.drawable.f43,R.drawable.f44, R.drawable.f45,R.drawable.f46,R.drawable.f47,R.drawable.f48,R.drawable.f49,R.drawable.f50,R.drawable.f51,R.drawable.f52,R.drawable.f53, R.drawable.f54,R.drawable.f55,R.drawable.f56,R.drawable.f57,R.drawable.f58,R.drawable.f59,R.drawable.f60,R.drawable.f61,R.drawable.f62, R.drawable.f63,R.drawable.f64,R.drawable.f65,R.drawable.f66,R.drawable.f67,R.drawable.f68,R.drawable.f69,R.drawable.f70,R.drawable.f71 }; private Context mcontext; private String[] mSmileyTexts; private Pattern mPattern; private HashMap<String, Integer> mSmileyToRes; public SmileyParser(Context mcontext) { super(); this.mcontext = mcontext; this.mSmileyTexts = mcontext.getResources().getStringArray(R.array.default_smiley_name); this.mPattern = buildPattern(); ; this.mSmileyToRes = buildSmileyToRes(); } private static SmileyParser instance; public static SmileyParser getInstance() { return instance; } public static void init(Context context) { instance = new SmileyParser(context); } private HashMap<String, Integer> buildSmileyToRes() { // TODO Auto-generated method stub 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() {//建立正则表达式... // TODO Auto-generated method stub 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; } }
<array name="default_smiley_name"> <item>[织]</item> <item>[神马]</item> <item>[浮云]</item> <item>[给力]</item> <item>[围观]</item> <item>[威武]</item> <item>[熊猫]</item> <item>[兔子]</item> <item>[奥特曼]</item> <item>[囧]</item> <item>[互粉]</item> <item>[礼物]</item> <item>[呵呵]</item> <item>[嘻嘻]</item> <item>[哈哈]</item> <item>[可爱]</item> <item>[可怜]</item> <item>[挖鼻屎]</item> <item>[吃惊]</item> <item>[害羞]</item> <item>[挤眼]</item> <item>[闭嘴]</item> <item>[鄙视]</item> <item>[爱你]</item> <item>[泪]</item> <item>[偷笑]</item> <item>[亲亲]</item> <item>[生病]</item> <item>[太开心]</item> <item>[懒得理你]</item> <item>[右哼哼]</item> <item>[左哼哼]</item> <item>[嘘]</item> <item>[衰]</item> <item>[委屈]</item> <item>[吐]</item> <item>[打哈气]</item> <item>[抱抱]</item> <item>[怒]</item> <item>[疑问]</item> <item>[馋嘴]</item> <item>[拜拜]</item> <item>[思考]</item> <item>[汗]</item> <item>[困]</item> <item>[睡觉]</item> <item>[钱]</item> <item>[失望]</item> <item>[酷]</item> <item>[花心]</item> <item>[哼]</item> <item>[鼓掌]</item> <item>[晕]</item> <item>[悲伤]</item> <item>[抓狂]</item> <item>[黑线]</item> <item>[阴险]</item> <item>[怒骂]</item> <item>[心]</item> <item>[伤心]</item> <item>[猪头]</item> <item>[ok]</item> <item>[耶]</item> <item>[good]</item> <item>[不要]</item> <item>[赞]</item> <item>[来]</item> <item>[弱]</item> <item>[蜡烛]</item> <item>[钟]</item> <item>[蛋糕]</item> <item>[话筒]</item> </array>
//<img src='http://XXX.png' />的情况 ImageGetter imgGetter = new Html.ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable = null; Log.d("Image Path", source); URL url; try { url = new URL(source); drawable = Drawable.createFromStream(url.openStream(), ""); } catch (Exception e) { return null; } drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; } }; ......... TextView description=(TextView)findViewById(R.id.description); description.setText(Html.fromHtml(htmlStr,imgGetter,null));
//<img src='" + R.drawable.icon + "' />的情况
ImageGetter imgGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
Drawable drawable = getResources().getDrawable(id);
//int height = fontH;
//int width = (int) ((float) drawable.getIntrinsicWidth() / (float) drawable.getIntrinsicHeight()) * fontH;
//drawable.setBounds(0, 0, width, height);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
}
};
转载至:http://gundumw100.iteye.com/blog/904107