当前位置: 代码迷 >> Android >> (转摘)Android腾讯微博客户端开发6:给用户加VIP认证以及美化微博显示内容
  详细解决方案

(转摘)Android腾讯微博客户端开发6:给用户加VIP认证以及美化微博显示内容

热度:17   发布时间:2016-05-01 18:44:04.0
(转摘)Android腾讯微博客户端开发六:给用户加VIP认证以及美化微博显示内容

##之间的内容是代表一个话题,@和:之间的是转发这条微博的人,也高亮显示,||代表上一次转发此微博的。url地址也高亮显示。


这个是什么组件啊?是TextView,不会吧?TextView能显示图片吗?肯定能呀。



主要是用到了SpannableString,ForegroundColorSpan,ImageSpan这3个类.

Java代码 复制代码?收藏代码
  1. String?origtext?=?data.getString("origtext"); ??
  2. ????????????????????SpannableString?spannable?=?new?SpannableString(origtext); ??
  3. ???????????????????? ??
  4. ????????????????????spannable?=?TextUtil.decorateFaceInStr(spannable,?RegexUtil.getStartAndEndIndex(origtext,?Pattern.compile("\\/[\u4e00-\u9fa5a-zA-Z]{1,3}")),?getResources());//解析成本地表情 ??
  5. ????????????????????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代码 复制代码?收藏代码
  1. public?static?List<Map<String,Object>>?getStartAndEndIndex(String?sourceStr,Pattern?pattern){ ??
  2. ????????List<Map<String,Object>>?list?=?new?ArrayList<Map<String,Object>>(); ??
  3. ????????Matcher?matcher?=?pattern.matcher(sourceStr); ??
  4. ????????boolean?isFind?=?matcher.find(); ??
  5. ????????while?(isFind)?{ ??
  6. ????????????Map<String,Object>?map?=?new?HashMap<String,?Object>(); ??
  7. ????????????String?faceName?=?matcher.group().substring(1,matcher.group().length());//表情名称 ??
  8. ????????????map.put("startIndex",matcher.start()); ??
  9. ????????????map.put("endIndex",matcher.end()); ??
  10. ????????????map.put("faceName",faceName); ??
  11. ????????????list.add(map); ??
  12. ????????????isFind?=?matcher.find((Integer)map.get("endIndex")-1); ??
  13. ????????} ??
  14. ????????return?list; ??
  15. ????}??
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代码 复制代码?收藏代码
  1. public?static?SpannableString?decorateVipInStr(SpannableString?spannable,List<Map<String,Object>>?list,Resources?resources){ ??
  2. ????????int?size?=?list.size(); ??
  3. ????????Drawable?drawable?=?null; ??
  4. ????????if(list!=null&&list.size()>0){ ??
  5. ????????????for(int?i=0;i<size;i++){ ??
  6. ????????????????Map<String,Object>?map?=?list.get(i); ??
  7. ????????????????drawable?=?resources.getDrawable(R.drawable.vip); ??
  8. ????????????????drawable.setBounds(0,?0,?drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); ??
  9. ????????????????ImageSpan?span?=?new?ImageSpan(drawable,ImageSpan.ALIGN_BASELINE); ??
  10. ????????????????spannable.setSpan(span,?(Integer)map.get("startIndex"),?(Integer)map.get("endIndex"),?Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); ??
  11. ????????????} ??
  12. ????????} ??
  13. ????????return?spannable; ??
  14. ????} ??
  15. ???? ??
  16. ????public?static?SpannableString?decorateTopicInStr(SpannableString?spannable,List<Map<String,Object>>?list,Resources?resources){ ??
  17. ????????int?size?=?list.size(); ??
  18. ????????Drawable?drawable?=?null; ??
  19. ????????CharacterStyle?foregroundColorSpan=new?ForegroundColorSpan(Color.argb(255,?33,?92,?110)); ??
  20. ????????if(list!=null&&list.size()>0){ ??
  21. ????????????for(int?i=0;i<size;i++){ ??
  22. ????????????????Map<String,Object>?map?=?list.get(i); ??
  23. ????????????????spannable.setSpan(foregroundColorSpan,?(Integer)map.get("startIndex"),?(Integer)map.get("endIndex"),?Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); ??
  24. ????????????} ??
  25. ????????} ??
  26. ????????return?spannable; ??
  27. ????}??
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
  相关解决方案