##之间的内容是代表一个话题,@和:之间的是转发这条微博的人,也高亮显示,||代表上一次转发此微博的。url地址也高亮显示。
这个是什么组件啊?是TextView,不会吧?TextView能显示图片吗?肯定能呀。
主要是用到了SpannableString,ForegroundColorSpan,ImageSpan这3个类.
- 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());//高亮显示微薄转发,回复者昵称
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());//高亮显示微薄转发,回复者昵称
- 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;
- }
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 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;
- }
- 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;
- }
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;
}
http://helloandroid.iteye.com/blog/1136290