/**
* 根據文本替換成圖片
* @param text
*
@return
*/
public CharSequence strToSmiley(CharSequence text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Pattern mPattern = Pattern.compile("(
\\[[0-9]{1,3}\\])"); Matcher matcher = mPattern.matcher(text); String s=""; int resId=0; while (matcher.find()) { for (int j = 0; j <FaceTool.facelist.size(); j++) { s=FaceTool.facelist.get(j).get(matcher.group())+""; if(!s.equalsIgnoreCase("null")) resId=Integer.parseInt(s); } Drawable drawable = this.getResources().getDrawable(resId); drawable.setBounds(0, 0, 25, 25);//這裏設置圖片的大小 ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM); builder.setSpan(imageSpan, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return builder; }