在这里插入图片描述

[历史归档] 本文原发布于 cstriker1407.info 个人博客,内容为历史存档,仅供参考。
发布时间: 2013-11-10 | 标题:android图文混排 | 分类: 编程 / android | 标签: android · 图文混排


android图文混排

以下是一个 Android 图文混排工具类的实现,核心思路是用正则匹配文本中的表情标记(如 [smile]),再用 SpannableStringBuilder + ImageSpan 将其替换为对应的 Bitmap 图片。

public class FaceUtils
{
 private static HashMap<String, Bitmap>faceMap = null;
 private static void initMaps(Context context)
 {
  faceMap = new HashMap<String, Bitmap>();
  String[] faces_res_name = context.getResources().getStringArray(R.array.faces_res_name);
  String[] faces_alias = context.getResources().getStringArray(R.array.faces_alias);
  for(int i=0;i<faces_res_name.length;i++)
  {
   int id = context.getResources().getIdentifier(faces_res_name, "drawable", context.getPackageName());
   Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id);
   int w = context.getResources().getDimensionPixelOffset(R.dimen.face_img_w_big);
   bmp = Bitmap.createScaledBitmap(bmp, w, w, true);
   faceMap.put(faces_alias, bmp);
  }
 }
 public static SpannableStringBuilder decodeFaceInText(Context context, String message)
 {
  if (null == faceMap)
  {
   initMaps(context);
  }
  SpannableStringBuilder ssb = new SpannableStringBuilder();
  ssb.append(message);
  Pattern pattern = Pattern.compile("\\[\\w+\\]");
  Matcher matcher = pattern.matcher(message);
  int index = 0, length = 0;
  while (matcher.find())
  {
   String faceFind = matcher.group();
   index = message.indexOf(faceFind,index);
   length = faceFind.length();
   Bitmap map = faceMap.get(faceFind);
   if (map != null)
   {
    ssb.setSpan(new ImageSpan(context,map)
    , index
    , index + length
    , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
   }
   index = index + length;
  }
  return ssb;
 }
 /**
  * 翻译文字中的表情
  */
 private static boolean test = true;
 public static SpannableStringBuilder translateFaceInText(Context context, String message)
 {
  if (test)
  {
   message = "fasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafsfasafdsafdsafdsafdsafdsafdsafshellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]535435324242123[4234]53543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212334535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242123423453543532424212342345354353242421234234535435324242";
  }
  return decodeFaceInText(context, message);
 }
}
Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐