Search in sources :

Example 46 with ImageSpan

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

the class HtmlToSpannedConverter method startImg.

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) {
        d = Resources.getSystem().getDrawable(com.android.internal.R.drawable.unknown_image);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    }
    int len = text.length();
    text.append("");
    text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Also used : Drawable(android.graphics.drawable.Drawable) ImageSpan(android.text.style.ImageSpan)

Example 47 with ImageSpan

use of android.text.style.ImageSpan in project NetGuard by M66B.

the class ActivityMain method markPro.

private void markPro(MenuItem menu, String sku) {
    if (sku == null || !IAB.isPurchased(sku, this)) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean dark = prefs.getBoolean("dark_theme", false);
        SpannableStringBuilder ssb = new SpannableStringBuilder("  " + menu.getTitle());
        ssb.setSpan(new ImageSpan(this, dark ? R.drawable.ic_shopping_cart_white_24dp : R.drawable.ic_shopping_cart_black_24dp), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        menu.setTitle(ssb);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan)

Example 48 with ImageSpan

use of android.text.style.ImageSpan in project xabber-android by redsolution.

the class EmojiconHandler method addEmojis.

/**
 * Convert emoji characters of the given Spannable to the according emojicon.
 *
 * @param context
 * @param text
 * @param emojiSize
 * @param index
 * @param length
 */
public static void addEmojis(Context context, Spannable text, int emojiSize, int index, int length) {
    int textLength = text.length();
    int textLengthToProcessMax = textLength - index;
    int textLengthToProcess = length < 0 || length >= textLengthToProcessMax ? textLength : (length + index);
    // remove spans throughout all text
    EmojiconSpan[] oldSpans = text.getSpans(0, textLength, EmojiconSpan.class);
    for (int i = 0; i < oldSpans.length; i++) {
        text.removeSpan(oldSpans[i]);
    }
    int skip;
    for (int i = index; i < textLengthToProcess; i += skip) {
        skip = 0;
        int icon = 0;
        char c = text.charAt(i);
        if (isSoftBankEmoji(c)) {
            icon = getSoftbankEmojiResource(c);
            skip = icon == 0 ? 0 : 1;
        }
        if (icon == 0) {
            int unicode = Character.codePointAt(text, i);
            skip = Character.charCount(unicode);
            if (unicode > 0xff) {
                icon = getEmojiResource(context, unicode);
            }
            if (icon == 0 && i + skip < textLengthToProcess) {
                int followUnicode = Character.codePointAt(text, i + skip);
                if (followUnicode == 0x20e3) {
                    int followSkip = Character.charCount(followUnicode);
                    switch(unicode) {
                        case 0x0031:
                            icon = R.drawable.emoji_0031;
                            break;
                        case 0x0032:
                            icon = R.drawable.emoji_0032;
                            break;
                        case 0x0033:
                            icon = R.drawable.emoji_0033;
                            break;
                        case 0x0034:
                            icon = R.drawable.emoji_0034;
                            break;
                        case 0x0035:
                            icon = R.drawable.emoji_0035;
                            break;
                        case 0x0036:
                            icon = R.drawable.emoji_0036;
                            break;
                        case 0x0037:
                            icon = R.drawable.emoji_0037;
                            break;
                        case 0x0038:
                            icon = R.drawable.emoji_0038;
                            break;
                        case 0x0039:
                            icon = R.drawable.emoji_0039;
                            break;
                        case 0x0030:
                            icon = R.drawable.emoji_0030;
                            break;
                        case 0x0023:
                            icon = R.drawable.emoji_0023;
                            break;
                        default:
                            followSkip = 0;
                            break;
                    }
                    skip += followSkip;
                } else {
                    int followSkip = Character.charCount(followUnicode);
                    switch(unicode) {
                        case 0x1f1ef:
                            icon = (followUnicode == 0x1f1f5) ? R.drawable.emoji_1f1ef_1f1f5 : 0;
                            break;
                        case 0x1f1fa:
                            icon = (followUnicode == 0x1f1f8) ? R.drawable.emoji_1f1fa_1f1f8 : 0;
                            break;
                        case 0x1f1eb:
                            icon = (followUnicode == 0x1f1f7) ? R.drawable.emoji_1f1eb_1f1f7 : 0;
                            break;
                        case 0x1f1e9:
                            icon = (followUnicode == 0x1f1ea) ? R.drawable.emoji_1f1e9_1f1ea : 0;
                            break;
                        case 0x1f1ee:
                            icon = (followUnicode == 0x1f1f9) ? R.drawable.emoji_1f1ee_1f1f9 : 0;
                            break;
                        case 0x1f1ec:
                            icon = (followUnicode == 0x1f1e7) ? R.drawable.emoji_1f1ec_1f1e7 : 0;
                            break;
                        case 0x1f1ea:
                            icon = (followUnicode == 0x1f1f8) ? R.drawable.emoji_1f1ea_1f1f8 : 0;
                            break;
                        case 0x1f1f7:
                            icon = (followUnicode == 0x1f1fa) ? R.drawable.emoji_1f1f7_1f1fa : 0;
                            break;
                        case 0x1f1e8:
                            icon = (followUnicode == 0x1f1f3) ? R.drawable.emoji_1f1e8_1f1f3 : 0;
                            break;
                        case 0x1f1f0:
                            icon = (followUnicode == 0x1f1f7) ? R.drawable.emoji_1f1f0_1f1f7 : 0;
                            break;
                        default:
                            followSkip = 0;
                            break;
                    }
                    skip += followSkip;
                }
            }
        }
        if (icon > 0) {
            Drawable myIcon = context.getResources().getDrawable(icon);
            myIcon.setBounds(0, 0, emojiSize, emojiSize);
            text.setSpan(new ImageSpan(myIcon, DynamicDrawableSpan.ALIGN_BOTTOM), i, i + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
Also used : Drawable(android.graphics.drawable.Drawable) ImageSpan(android.text.style.ImageSpan)

Example 49 with ImageSpan

use of android.text.style.ImageSpan in project xabber-android by redsolution.

the class Emoticons method getSmiledText.

/**
 * @param context
 * @param spannable
 * @return Whether smiles have been added into <code>spannable</code>.
 */
public static boolean getSmiledText(Context context, Spannable spannable, TextView textView) {
    boolean hasChanges = false;
    Map<Pattern, Integer> emoticons = SettingsManager.interfaceSmiles();
    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) {
                Drawable myIcon = context.getResources().getDrawable(entry.getValue());
                myIcon.setBounds(0, 0, textView.getLineHeight(), textView.getLineHeight());
                spannable.setSpan(new ImageSpan(myIcon, DynamicDrawableSpan.ALIGN_BOTTOM), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                hasChanges = true;
            }
        }
    }
    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 50 with ImageSpan

use of android.text.style.ImageSpan in project Klyph by jonathangerbaud.

the class EmojiUtil method getSpannableForText.

public static Spannable getSpannableForText(Context context, String text, boolean addLinks) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(text);
    Linkify.addLinks(ssb, Linkify.WEB_URLS);
    for (String key : EMOJIS.keySet()) {
        int index = text.indexOf(key);
        while (index != -1) {
            if (ssb.getSpans(index, index + 1, Object.class).length == 0)
                /* && ssb.getSpans(index, index + 1, URLSpan.class).length == 0*/
                ssb.setSpan(new ImageSpan(context, EMOJIS.get(key)), index, index + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            index = text.indexOf(key, index + key.length());
        }
    }
    if (!addLinks) {
        URLSpan[] spans = ssb.getSpans(0, text.length() - 1, URLSpan.class);
        for (URLSpan urlSpan : spans) {
            ssb.removeSpan(urlSpan);
        }
    }
    return ssb;
}
Also used : URLSpan(android.text.style.URLSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) 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