Search in sources :

Example 71 with ImageSpan

use of android.text.style.ImageSpan in project XobotOS by xamarin.

the class SmileyParser method getSpannableString.

/**
     * Retrieves the parsed text as a spannable string object.
     * @param context the context for fetching smiley resources.
     * @return the spannable string as CharSequence.
     */
public CharSequence getSpannableString(Context context) {
    SpannableStringBuilder builder = new SpannableStringBuilder();
    if (getPartCount() == 0) {
        return "";
    }
    // should have only one part since we parse smiley only
    Part part = getPart(0);
    ArrayList<Token> tokens = part.getTokens();
    int len = tokens.size();
    for (int i = 0; i < len; i++) {
        Token token = tokens.get(i);
        int start = builder.length();
        builder.append(token.getRawText());
        if (token.getType() == AbstractMessageParser.Token.Type.SMILEY) {
            int resid = mRes.getSmileyRes(token.getRawText());
            if (resid != -1) {
                builder.setSpan(new ImageSpan(context, resid), start, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return builder;
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan)

Example 72 with ImageSpan

use of android.text.style.ImageSpan in project RichText by zzhoujay.

the class CachedSpannedParser method handleImage.

private int handleImage(SpannableStringBuilder ssb, ImageGetterWrapper imageGetterWrapper, RichTextConfig config, boolean cached) {
    if (cached) {
        ClickableImageSpan[] cis = ssb.getSpans(0, ssb.length(), ClickableImageSpan.class);
        if (cis != null && cis.length > 0) {
            for (ClickableImageSpan ci : cis) {
                int start = ssb.getSpanStart(ci);
                int end = ssb.getSpanEnd(ci);
                ssb.removeSpan(ci);
                OnImageClickListener onImageClickListener = null;
                OnImageLongClickListener onImageLongClickListener = null;
                if (config.clickable > 0) {
                    onImageClickListener = config.onImageClickListener;
                    onImageLongClickListener = config.onImageLongClickListener;
                }
                Drawable drawable = imageGetterWrapper.getDrawable(ci.getSource());
                if (drawable == null) {
                    drawable = new ColorDrawable(Color.TRANSPARENT);
                }
                ClickableImageSpan nci = new ClickableImageSpan(drawable, ci, onImageClickListener, onImageLongClickListener);
                ssb.setSpan(nci, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return cis.length;
        }
    } else if (!config.noImage) {
        ImageSpan[] iss = ssb.getSpans(0, ssb.length(), ImageSpan.class);
        if (iss != null && iss.length > 0) {
            ArrayList<String> imageUrls = new ArrayList<>(iss.length);
            for (int i = 0; i < iss.length; i++) {
                ImageSpan imageSpan = iss[i];
                String imageUrl = imageSpan.getSource();
                imageUrls.add(imageUrl);
                int start = ssb.getSpanStart(imageSpan);
                int end = ssb.getSpanEnd(imageSpan);
                ClickableSpan[] clickableSpans = ssb.getSpans(start, end, ClickableSpan.class);
                if (clickableSpans != null && clickableSpans.length != 0) {
                    for (ClickableSpan cs : clickableSpans) {
                        ssb.removeSpan(cs);
                    }
                }
                OnImageClickListener onImageClickListener = null;
                OnImageLongClickListener onImageLongClickListener = null;
                if (config.clickable > 0) {
                    onImageClickListener = config.onImageClickListener;
                    onImageLongClickListener = config.onImageLongClickListener;
                }
                Drawable drawable = imageGetterWrapper.getDrawable(imageUrl);
                if (drawable == null) {
                    drawable = new ColorDrawable(Color.TRANSPARENT);
                }
                ClickableImageSpan cacheImageSpan = new ClickableImageSpan(drawable, imageUrls, i, onImageClickListener, onImageLongClickListener);
                ssb.removeSpan(imageSpan);
                ssb.setSpan(cacheImageSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return iss.length;
        }
    }
    return 0;
}
Also used : OnImageClickListener(com.zzhoujay.richtext.callback.OnImageClickListener) ColorDrawable(android.graphics.drawable.ColorDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) ArrayList(java.util.ArrayList) OnImageLongClickListener(com.zzhoujay.richtext.callback.OnImageLongClickListener) ClickableImageSpan(com.zzhoujay.richtext.spans.ClickableImageSpan) ClickableSpan(android.text.style.ClickableSpan) ClickableImageSpan(com.zzhoujay.richtext.spans.ClickableImageSpan) ImageSpan(android.text.style.ImageSpan)

Example 73 with ImageSpan

use of android.text.style.ImageSpan in project android_frameworks_base by DirtyUnicorns.

the class SmileyParser method getSpannableString.

/**
     * Retrieves the parsed text as a spannable string object.
     * @param context the context for fetching smiley resources.
     * @return the spannable string as CharSequence.
     */
public CharSequence getSpannableString(Context context) {
    SpannableStringBuilder builder = new SpannableStringBuilder();
    if (getPartCount() == 0) {
        return "";
    }
    // should have only one part since we parse smiley only
    Part part = getPart(0);
    ArrayList<Token> tokens = part.getTokens();
    int len = tokens.size();
    for (int i = 0; i < len; i++) {
        Token token = tokens.get(i);
        int start = builder.length();
        builder.append(token.getRawText());
        if (token.getType() == AbstractMessageParser.Token.Type.SMILEY) {
            int resid = mRes.getSmileyRes(token.getRawText());
            if (resid != -1) {
                builder.setSpan(new ImageSpan(context, resid), start, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return builder;
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan)

Example 74 with ImageSpan

use of android.text.style.ImageSpan in project wechat by motianhuo.

the class SmileUtils method addSmiles.

/**
	 * 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;
                Drawable drawable = context.getResources().getDrawable(entry.getValue());
                // 这里设置图片的大小
                drawable.setBounds(0, 0, 50, 50);
                ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
                spannable.setSpan(imageSpan, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Drawable(android.graphics.drawable.Drawable) ImageSpan(android.text.style.ImageSpan)

Example 75 with ImageSpan

use of android.text.style.ImageSpan in project android_frameworks_base by AOSPA.

the class RecentsCreateFragment method appendDrawable.

private static void appendDrawable(SpannableStringBuilder b, Drawable d) {
    final int length = b.length();
    b.append("〉");
    b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Also used : ImageSpan(android.text.style.ImageSpan)

Aggregations

ImageSpan (android.text.style.ImageSpan)98 SpannableStringBuilder (android.text.SpannableStringBuilder)53 Drawable (android.graphics.drawable.Drawable)29 ForegroundColorSpan (android.text.style.ForegroundColorSpan)23 BackgroundColorSpan (android.text.style.BackgroundColorSpan)14 TextView (android.widget.TextView)14 SpannableString (android.text.SpannableString)12 View (android.view.View)11 UnderlineSpan (android.text.style.UnderlineSpan)10 RelativeSizeSpan (android.text.style.RelativeSizeSpan)7 StyleSpan (android.text.style.StyleSpan)7 Bitmap (android.graphics.Bitmap)6 LinearLayout (android.widget.LinearLayout)6 Application (android.app.Application)5 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)5 CharacterStyle (android.text.style.CharacterStyle)5 SuggestionSpan (android.text.style.SuggestionSpan)5 URLSpan (android.text.style.URLSpan)5 MenuItem (android.view.MenuItem)5 Button (android.widget.Button)5