这个是什么组件啊?是TextView,不会吧?TextView能显示图片吗?肯定能呀。
主要是用到了SpannableString,ForegroundColorSpan,ImageSpan这3个类.
Java代码
String origtext = data.getString("origtext");
SpannableString spannable = new SpannableString(origtext);
spannable = TextUtil.decorateFaceInStr(spannable, RegexUtil.getStartAndEndIndex(origtext, Pattern.compile("\\/[\u4e00-\u9fa5a-zA-Z]{1,3}")), getResources());//解析成本地表情
spannable = TextUtil.decorateRefersInStr(spannable, RegexUtil.getStartAndEndIndex(origtext, Pattern.compile("@.*:")), getResources());//高亮显示微薄转发,回复者昵称
String origtext = data.getString("origtext");
SpannableString spannable = new SpannableString(origtext);
spannable = TextUtil.decorateFaceInStr(spannable, RegexUtil.getStartAndEndIndex(origtext, Pattern.compile("\\/[\u4e00-\u9fa5a-zA-Z]{1,3}")), getResources());//解析成本地表情
spannable = TextUtil.decorateRefersInStr(spannable, RegexUtil.getStartAndEndIndex(origtext, Pattern.compile("@.*:")), getResources());//高亮显示微薄转发,回复者昵称
Java代码
public static List<Map<String,Object>> getStartAndEndIndex(String sourceStr,Pattern pattern){
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Matcher matcher = pattern.matcher(sourceStr);
boolean isFind = matcher.find();
while (isFind) {
Map<String,Object> map = new HashMap<String, Object>();
String faceName = matcher.group().substring(1,matcher.group().length());//表情名称
map.put("startIndex",matcher.start());
map.put("endIndex",matcher.end());
map.put("faceName",faceName);
list.add(map);
isFind = matcher.find((Integer)map.get("endIndex")-1);
}
return list;
}
public static List<Map<String,Object>> getStartAndEndIndex(String sourceStr,Pattern pattern){
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Matcher matcher = pattern.matcher(sourceStr);
boolean isFind = matcher.find();
while (isFind) {
Map<String,Object> map = new HashMap<String, Object>();
String faceName = matcher.group().substring(1,matcher.group().length());//表情名称
map.put("startIndex",matcher.start());
map.put("endIndex",matcher.end());
map.put("faceName",faceName);
list.add(map);
isFind = matcher.find((Integer)map.get("endIndex")-1);
}
return list;
}
Java代码
public static SpannableString decorateVipInStr(SpannableString spannable,List<Map<String,Object>> list,Resources resources){
int size = list.size();
Drawable drawable = null;
if(list!=null&&list.size()>0){
for(int i=0;i<size;i++){
Map<String,Object> map = list.get(i);
drawable = resources.getDrawable(R.drawable.vip);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
spannable.setSpan(span, (Integer)map.get("startIndex"), (Integer)map.get("endIndex"), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return spannable;
}
public static SpannableString decorateTopicInStr(SpannableString spannable,List<Map<String,Object>> list,Resources resources){
int size = list.size();
Drawable drawable = null;
CharacterStyle foregroundColorSpan=new ForegroundColorSpan(Color.argb(255, 33, 92, 110));
if(list!=null&&list.size()>0){
for(int i=0;i<size;i++){
Map<String,Object> map = list.get(i);
spannable.setSpan(foregroundColorSpan, (Integer)map.get("startIndex"), (Integer)map.get("endIndex"), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return spannable;
}