boolean replace(ImageSpan by) { int start = builder.getSpanStart(span); int end = builder.getSpanEnd(span); if (start == -1) return true; builder.removeSpan(span); builder.setSpan( by, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE ); target.setText(builder); return true; }
@Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { // TODO Auto-generated method stub // �����ǩ<img> if (tag.toLowerCase().equals("img")) { // ��ȡ���� int len = output.length(); // ��ȡͼƬ��ַ ImageSpan[] images = output.getSpans(len-1, len, ImageSpan.class); String imgURL = images[0].getSource(); // ʹͼƬ�ɵ������������¼� output.setSpan(new ImageClick(context, imgURL), len-1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } }
public static void emoticonDisplay(Context context, Spannable spannable, String emoticonName, int fontSize, int start, int end) { Drawable drawable = getDrawableFromAssets(context, emoticonName); if (drawable != null) { int itemHeight; int itemWidth; if (fontSize == WRAP_DRAWABLE) { itemHeight = drawable.getIntrinsicHeight(); itemWidth = drawable.getIntrinsicWidth(); } else { itemHeight = fontSize; itemWidth = fontSize; } drawable.setBounds(0, 0, itemHeight, itemWidth); ImageSpan imageSpan = new ImageSpan(drawable); spannable.setSpan(imageSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } }
private ChipSpan createAndPutChipForUser(TLRPC.User user) { LayoutInflater lf = (LayoutInflater) ApplicationLoader.applicationContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); View textView = lf.inflate(R.layout.group_create_bubble, null); TextView text = (TextView)textView.findViewById(R.id.bubble_text_view); String name = UserObject.getUserName(user); if (name.length() == 0 && user.phone != null && user.phone.length() != 0) { name = PhoneFormat.getInstance().format("+" + user.phone); } text.setText(name + ", "); int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); textView.measure(spec, spec); textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight()); Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(b); canvas.translate(-textView.getScrollX(), -textView.getScrollY()); textView.draw(canvas); textView.setDrawingCacheEnabled(true); Bitmap cacheBmp = textView.getDrawingCache(); Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true); textView.destroyDrawingCache(); final BitmapDrawable bmpDrawable = new BitmapDrawable(b); bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight()); SpannableStringBuilder ssb = new SpannableStringBuilder(""); ChipSpan span = new ChipSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE); allSpans.add(span); selectedContacts.put(user.id, span); for (ImageSpan sp : allSpans) { ssb.append("<<"); ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE); } userSelectEditText.setText(ssb); userSelectEditText.setSelection(ssb.length()); return span; }
public static ImageSpan getTextWithBackground(Drawable background, final int textSize , final int padding, final int color){ return new ImageSpan(background){ @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { paint.setTextSize(textSize + padding); int len = Math.round(paint.measureText(text, start, end)); getDrawable().setBounds(0, 0, len, (int) (1.5f * getTextMaxHeight(paint,textSize))); Drawable b = getDrawable(); canvas.save(); int transY = ((bottom-top) - b.getBounds().bottom)/2+top; canvas.translate(x , transY); b.draw(canvas); canvas.restore(); paint.setColor(color); paint.setTextSize(textSize); canvas.drawText(text.subSequence(start, end).toString(), x+ padding ,y, paint); } }; }
private CharSequence replace(CharSequence text) { try { SpannableStringBuilder builder = new SpannableStringBuilder(text); Pattern pattern = buildPattern(); Matcher matcher = pattern.matcher(text); while (matcher.find()) { if (BaseApplication.mEmoticonsId.containsKey(matcher.group())) { int id = BaseApplication.mEmoticonsId.get(matcher.group()); Bitmap bitmap = BitmapFactory.decodeResource( getResources(), id); if (bitmap != null) { ImageSpan span = new ImageSpan(getContext(), bitmap); builder.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return builder; } catch (Exception e) { return text; } }
@Override public void handleTagNode(TagNode node, SpannableStringBuilder builder, int start, int end, SpanStack stack) { String src = node.getAttributeByName("src"); builder.append("\uFFFC"); Bitmap bitmap = loadBitmap(src); if (bitmap != null) { Drawable drawable = new BitmapDrawable(bitmap); drawable.setBounds(0, 0, bitmap.getWidth() - 1, bitmap.getHeight() - 1); stack.pushSpan( new ImageSpan(drawable), start, builder.length() ); } }
@NonNull private SpannableStringBuilder getSpannable() { SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getResources().getString(R.string.content_cn)); Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher); drawable.setBounds(0, 0, 35, 35); spannableStringBuilder.setSpan(new ImageSpan(drawable) , 36, 37, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableStringBuilder.setSpan(new ImageSpan(drawable) , 37, 38, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableStringBuilder.setSpan(new ImageSpan(drawable) , 38, 39, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableStringBuilder.setSpan(new ImageSpan(drawable) , 39, 40, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); // ItalicReplacementSpan italicSpan = new ItalicReplacementSpan(-0.25f); // StrokeSpan strokeSpan = new StrokeSpan(Color.BLUE, Color.YELLOW, 20); // spannableStringBuilder.setSpan(strokeSpan, 0, spannableStringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); return spannableStringBuilder; }
/** 显示文本和表情 */ public static void setText(TextView textView, String text) { Context context = textView.getContext(); Resources resources = context.getResources(); SpannableString ss = new SpannableString(text); // 正则表达式: [高兴] Pattern p = Pattern.compile("\\[([A-Za-z\u4E00-\u9FA5]+)\\]"); Matcher matcher = p.matcher(ss); while (matcher.find()) { // 匹配到一个表情字符串 String emoji = matcher.group(); // 过滤非表情符,比如: [xxx] if (EMOJI_DATAS.containsKey(emoji)) { // 是表情才处理 // System.out.println("----------" + emoji); // 指定了一张图片 Bitmap bitmap = BitmapFactory.decodeResource(resources, EMOJI_DATAS.get(emoji)); bitmap = Global.createBitmap(bitmap, Global.dp2px(20)); // 图片的宽高为20dp ImageSpan span = new ImageSpan(context, bitmap, ImageSpan.ALIGN_BOTTOM); int start = matcher.start(); int end = matcher.end(); ss.setSpan(span, start, end, 0); } } textView.setText(ss); }
public static SpannableStringBuilder parseEmoji(Context context,String content){ int size = UIUtils.dip2Px(25,context); //里[smiley_00]ff[smiley_78] SpannableStringBuilder builder = new SpannableStringBuilder(content); //正则表达式 Pattern pattern = Pattern.compile("\\[smiley_(.*?)\\]"); Matcher matcher = pattern.matcher(content); while(matcher.find()){ String emo = matcher.group();//[smiley_00] emo = emo.substring(1, emo.length()-1); //获取表情的id int id = context.getResources().getIdentifier(emo, "drawable", context.getPackageName()); if(id != 0 ){ Drawable drawable = context.getResources().getDrawable(id); // 必须设置图片大小 drawable.setBounds(0, 0, size, size); // 包装一下图片 ImageSpan span = new ImageSpan(drawable); // 把文字替换成图片 builder.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return builder; }
public void render(final TextView textView, final Context context, String line) { final Handler handler = new Handler(Looper.getMainLooper()); final SpannableString text = new SpannableString(" " + line + "\n"); handler.post(new Runnable() { @Override public void run() { //set the image tag behind (left of) the text text.setSpan(new ImageSpan(context, IMAGE_TAG), 0, 1, 0); textView.append(text); } }); }
public static SpannableString getEmotionText(Context context, String source, int emotionSizeDp) { SpannableString spannableString = new SpannableString(source); Matcher matcher = Pattern.compile(REGEX_GROUP).matcher(spannableString); if (matcher.find()) { matcher.reset(); } while (matcher.find()) { String emojiStr = matcher.group(1); if (emojiStr != null) { ImageSpan imageSpan = getImageSpan(context, emojiStr, emotionSizeDp); if (imageSpan != null) { int start = matcher.start(1); spannableString.setSpan(imageSpan, start, emojiStr.length() + start, 33); } } } return spannableString; }
private void startImg(Editable text, Attributes attributes, HtmlCompat.ImageGetter img) { String src = attributes.getValue("", "src"); Drawable d = null; if (img != null) { d = img.getDrawable(src, attributes); } if (d == null) { Resources res = mContext.getResources(); d = res.getDrawable(R.drawable.unknown_image); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); } int len = text.length(); text.append("\uFFFC"); text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
public SpannableStringBuilder getFace(Context mContext, String content) { SpannableStringBuilder sb = new SpannableStringBuilder(); try { /** * 经过测试,虽然这里tempText被替换为png显示,但是但我单击发送按钮时,获取到輸入框的内容是tempText的值而不是png * 所以这里对这个tempText值做特殊处理 * 格式:[face/png/f_static_000.png],以方便判斷當前圖片是哪一個 * */ String tempText = "[" + content + "]"; sb.append(tempText); sb.setSpan( new ImageSpan(mContext, BitmapFactory.decodeStream(mContext.getAssets().open(content))), sb.length() - tempText.length(), sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (Exception e) { e.printStackTrace(); } return sb; }
@Override protected void bindContentView() { layoutDirection(); TextView bodyTextView = findViewById(R.id.nim_message_item_text_body); bodyTextView.setTextColor(isReceivedMessage() ? Color.BLACK : Color.WHITE); bodyTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClick(); } }); MoonUtil.identifyFaceExpression(NimUIKit.getContext(), bodyTextView, getDisplayText(), ImageSpan.ALIGN_BOTTOM); bodyTextView.setMovementMethod(LinkMovementMethod.getInstance()); bodyTextView.setOnLongClickListener(longClickListener); }
public void show(IMMessage newMessage) { if (newMessageTipLayout == null) { init(); } if (!TextUtils.isEmpty(newMessage.getFromAccount())) { newMessageTipHeadImageView.loadBuddyAvatar(newMessage.getFromAccount()); } else { newMessageTipHeadImageView.resetImageView(); } MoonUtil.identifyFaceExpression(context, newMessageTipTextView, TeamNotificationHelper.getMsgShowText(newMessage), ImageSpan.ALIGN_BOTTOM); newMessageTipLayout.setVisibility(View.VISIBLE); uiHandler.removeCallbacks(showNewMessageTipLayoutRunnable); uiHandler.postDelayed(showNewMessageTipLayoutRunnable, 5 * 1000); }
private static SpannableString replaceEmoticons(Context context, String value, float scale, int align) { if(TextUtils.isEmpty(value)) { value=""; } SpannableString mSpannableString = new SpannableString(value); Matcher matcher = EmojiManager.getPattern().matcher(value); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String emot = value.substring(start, end); Drawable d = getEmotDrawable(context, emot, scale); if (d != null) { ImageSpan span = new ImageSpan(d, align); mSpannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return mSpannableString; }
public static void replaceEmoticons(Context context, Editable editable, int start, int count) { if (count <= 0 || editable.length() < start + count) return; CharSequence s = editable.subSequence(start, start + count); Matcher matcher = EmojiManager.getPattern().matcher(s); while (matcher.find()) { int from = start + matcher.start(); int to = start + matcher.end(); String emot = editable.subSequence(from, to).toString(); Drawable d = getEmotDrawable(context, emot, SMALL_SCALE); if (d != null) { ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM); editable.setSpan(span, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } }
public static ImageSpan getInputAitSpan(String name, float textsize) { if (TextUtils.isEmpty(name)) { return null; } Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setAntiAlias(true); paint.setTextSize(textsize); Rect rect = new Rect(); paint.getTextBounds(name, 0, name.length(), rect); // 获取字符串在屏幕上的长度 int width = (int) (paint.measureText(name)); final Bitmap bmp = Bitmap.createBitmap(width, rect.height(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); canvas.drawText(name, rect.left, rect.height() - rect.bottom, paint); return new ImageSpan(NimUIKit.getContext(), bmp, ImageSpan.ALIGN_BOTTOM); }
public void insert(SpannableString message) { for (String emoji : emojiList) { Matcher matcher = getEmojiParser(message.toString(), emoji); Bitmap emojiBitmap = null; while (matcher.find()) { if (emojiBitmap == null) { Resources resources = context.getResources(); int resourceId = resources.getIdentifier(String.format("%1s_%2s", emojiResourcePrefix, emoji), EMOJI_TYPE, context.getPackageName()); Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId); emojiBitmap = Bitmap.createScaledBitmap(bitmap, textSize, textSize, true); bitmap.recycle(); } ImageSpan span = new ImageSpan(context, emojiBitmap, ImageSpan.ALIGN_BASELINE); message.setSpan(span, matcher.start(), matcher.end(), 0); } } }
private static void startImg(SpannableStringBuilder text, Attributes attributes, Html.ImageGetter img) { String src = attributes.getValue("", "src"); Drawable d = null; if (img != null) { d = img.getDrawable(src); } if (d == null) { d = Resources.getSystem(). getDrawable(android.R.drawable.ic_delete); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); } int len = text.length(); text.append("\uFFFC"); text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
public static void handlerEmojiText(EditText comment, String content, Context context) throws IOException { SpannableStringBuilder sb = new SpannableStringBuilder(content); String regex = "\\[(\\S+?)\\]"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(content); Iterator<Emoji> iterator; Emoji emoji = null; while (m.find()) { iterator = emojiList.iterator(); String tempText = m.group(); while (iterator.hasNext()) { emoji = iterator.next(); if (tempText.equals(emoji.getContent())) { //转换为Span并设置Span的大小 sb.setSpan(new ImageSpan(context, decodeSampledBitmapFromResource(context.getResources(), emoji.getImageUri() , dip2px(context, 18), dip2px(context, 18))), m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); break; } } } comment.setText(sb); comment.setSelection(sb.length()); }
public static void handlerEmojiText(TextView comment, String content, Context context) throws IOException { SpannableStringBuilder sb = new SpannableStringBuilder(content); String regex = "\\[(\\S+?)\\]"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(content); Iterator<Emoji> iterator; Emoji emoji = null; while (m.find()) { iterator = emojiList.iterator(); String tempText = m.group(); while (iterator.hasNext()) { emoji = iterator.next(); if (tempText.equals(emoji.getContent())) { //转换为Span并设置Span的大小 sb.setSpan(new ImageSpan(context, decodeSampledBitmapFromResource(context.getResources(), emoji.getImageUri() , dip2px(context, 50), dip2px(context, 50))), m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); break; } } } comment.setText(sb); }
/** * 讲带emoji表情的字符串解析成带表情的文本 * * @param text * @return */ public static CharSequence fromStringToEmoji(CharSequence text, Context context) { SpannableStringBuilder builder = new SpannableStringBuilder(text); Pattern mPattern = Pattern.compile(":(\\S+?):"); Matcher matcher = mPattern.matcher(text); Integer resId = null; while (matcher.find()) { resId = EmojiTranslate.emojiSrcMap.get(matcher.group(0)); if (resId != null && resId > 0) { Drawable drawable = context.getResources().getDrawable(resId); int w = (int) (drawable.getIntrinsicWidth() * 0.32); int h = (int) (drawable.getIntrinsicHeight() * 0.32); drawable.setBounds(0, 0, w, h);//这里设置图片的大小 ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE); builder.setSpan(imageSpan, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return builder; }
/** * 讲带emoji表情的字符串解析成带表情的文本 * * @param text * @return */ public static CharSequence fromStringToEmoji1(CharSequence text, Context context) { SpannableStringBuilder builder = new SpannableStringBuilder(text); Pattern mPattern = Pattern.compile(":(\\S+?):"); Matcher matcher = mPattern.matcher(text); Integer resId = null; while (matcher.find()) { resId = EmojiTranslate.emojiSrcMap.get(matcher.group(0)); if (resId != null && resId > 0) { Drawable drawable = context.getResources().getDrawable(resId); int w = (int) (drawable.getIntrinsicWidth() * 0.38); int h = (int) (drawable.getIntrinsicHeight() * 0.38); drawable.setBounds(0, 0, w, h);//这里设置图片的大小 ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE); builder.setSpan(imageSpan, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return builder; }
public static void replaceEmoticons(Context context, Editable editable, int start, int count) { if (count <= 0 || editable.length() < start + count) return; CharSequence s = editable.subSequence(start, start + count); Matcher matcher = EmoUtil.getPattern().matcher(s); while (matcher.find()) { int from = start + matcher.start(); int to = start + matcher.end(); String emot = editable.subSequence(from, to).toString(); Drawable d = getEmotDrawable(context, emot, DEF_SCALE); if (d != null) { ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM); editable.setSpan(span, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } }
/** * toSpannableString * @return SpannableString * @throws */ public static SpannableString toSpannableString(Context context, String text) { if (!TextUtils.isEmpty(text)) { SpannableString spannableString = new SpannableString(text); int start = 0; Pattern pattern = Pattern.compile("\\\\ue[a-z0-9]{3}", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String faceText = matcher.group(); String key = faceText.substring(1); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), context.getResources().getIdentifier(key, "drawable", context.getPackageName()), options); ImageSpan imageSpan = new ImageSpan(context, bitmap); int startIndex = text.indexOf(faceText, start); int endIndex = startIndex + faceText.length(); if (startIndex >= 0) spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); start = (endIndex - 1); } return spannableString; } else { return new SpannableString(""); } }
public static SpannableString toSpannableString(Context context, String text, SpannableString spannableString) { int start = 0; Pattern pattern = Pattern.compile("\\\\ue[a-z0-9]{3}", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String faceText = matcher.group(); String key = faceText.substring(1); BitmapFactory.Options options = new BitmapFactory.Options(); // options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), context.getResources() .getIdentifier(key, "drawable", context.getPackageName()), options); ImageSpan imageSpan = new ImageSpan(context, bitmap); int startIndex = text.indexOf(faceText, start); int endIndex = startIndex + faceText.length(); if (startIndex >= 0) spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); start = (endIndex - 1); } return spannableString; }
private CharSequence replace(String text) { try { SpannableString spannableString = new SpannableString(text); int start = 0; Pattern pattern = buildPattern(); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String faceText = matcher.group(); String key = faceText.substring(1); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), getContext().getResources().getIdentifier(key, "drawable", getContext().getPackageName()), options); ImageSpan imageSpan = new ImageSpan(getContext(), bitmap); int startIndex = text.indexOf(faceText, start); int endIndex = startIndex + faceText.length(); if (startIndex >= 0) spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); start = (endIndex - 1); } return spannableString; } catch (Exception e) { return text; } }
private int buildChip(String chip, SpannableStringBuilder builder, int index, int pos) { chipBuilder.setPosition(pos); View view = chipBuilder.getChip(inflater, chip); int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); view.measure(spec, spec); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); Bitmap bmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); canvas.translate(-view.getScrollX(), -view.getScrollY()); view.draw(canvas); view.setDrawingCacheEnabled(true); Bitmap cache = view.getDrawingCache(); Bitmap bitmap = cache.copy(Bitmap.Config.ARGB_8888, true); view.destroyDrawingCache(); BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap); drawable.setBounds(0, 0, view.getWidth(), view.getHeight()); builder.setSpan(new ImageSpan(drawable), index, index + chip.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.setSpan(new ChipClickSpan(callback), index, index + chip.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return index + chip.length() + 1; }
private static void startImg(Editable text, Attributes attributes, Html.ImageGetter img) { String src = attributes.getValue("", "src"); Drawable d = null; if (img != null) { d = img.getDrawable(src); } if (d == null) { //TODO kakayato tam drawabla hz zachem /*d = Resources.getSystem(). getDrawable(R.drawable.adf);*/ d = App.getContext().getResources().getDrawable(R.drawable.adf); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); } int len = text.length(); text.append("\uFFFC"); text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
public void show(MessageModel newMessage) { if (newMessageTipLayout == null) { init(); } if (!TextUtils.isEmpty(newMessage.getEntity().getSender())) { newMessageTipHeadImageView.loadBuddyAvatarbyuid(newMessage.getEntity().getSender(), R.drawable.circle_image_transparent); } else { newMessageTipHeadImageView.resetImageView(); } MoonUtil.identifyFaceExpression(context, newMessageTipTextView, newMessage.getEntity().getContent(), ImageSpan.ALIGN_BOTTOM); newMessageTipLayout.setVisibility(View.VISIBLE); uiHandler.removeCallbacks(showNewMessageTipLayoutRunnable); uiHandler.postDelayed(showNewMessageTipLayoutRunnable, 5 * 1000); }
/** * replace existing spannable with smiles * @param context * @param spannable * @return */ public static boolean addSmiles(Context context, Spannable spannable) { boolean hasChanges = false; for (Entry<Pattern, Integer> entry : emoticons.entrySet()) { Matcher matcher = entry.getKey().matcher(spannable); while (matcher.find()) { boolean set = true; for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) if (spannable.getSpanStart(span) >= matcher.start() && spannable.getSpanEnd(span) <= matcher.end()) spannable.removeSpan(span); else { set = false; break; } if (set) { hasChanges = true; spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return hasChanges; }
public static SpannableStringBuilder parse(Context mContext, String content, int itemWidth, int itemHeight) { SpannableStringBuilder sb = new SpannableStringBuilder(content); String regex = "\\[[^\\]]+\\]";//[图片] Pattern p = Pattern.compile(regex); Matcher m = p.matcher(content); while (m.find()) { String tempText = m.group(); if (tempText.equals("[图片]")) { continue; } String png = tempText.substring("[".length(), tempText.length() - "]".length()); try { ImageSpan ispan = new ImageSpan(mContext, BitmapFactory.decodeStream(mContext.getAssets().open(png))); ispan.getDrawable().setBounds(0, 0, itemWidth, itemHeight); sb.setSpan(ispan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (IOException e1) { e1.printStackTrace(); } } return sb; }