Example usage for android.text.style ImageSpan ImageSpan

List of usage examples for android.text.style ImageSpan ImageSpan

Introduction

In this page you can find the example usage for android.text.style ImageSpan ImageSpan.

Prototype

public ImageSpan(@NonNull Context context, @DrawableRes int resourceId) 

Source Link

Document

Constructs an ImageSpan from a Context and a resource id with the default alignment DynamicDrawableSpan#ALIGN_BOTTOM

Usage

From source file:Main.java

public static CharSequence addImageToText(Context context, CharSequence text, int resID, int start, int end) {
    SpannableStringBuilder builder = new SpannableStringBuilder(text);

    Drawable d = context.getResources().getDrawable(resID);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    builder.setSpan(span, text.length(), 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

    return builder;
}

From source file:Main.java

public static SpannableString StringToBitMap(Context context, String msg) {
    SpannableString ss = new SpannableString(msg);
    Pattern p = Pattern.compile("/mnt/sdcard/.+?\\.\\w{3}");
    Matcher m = p.matcher(msg);//  w w w .  j  av a2 s . c o  m
    while (m.find()) {
        Bitmap bitmap = BitmapFactory.decodeFile(m.group());
        ImageSpan imageSpan = new ImageSpan(context, bitmap);
        ss.setSpan(imageSpan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return ss;
}

From source file:Main.java

public static CharSequence replace(Context ctx, String text) {
    SpannableString spannableString = new SpannableString(text);
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String factText = matcher.group();
        String key = factText.substring(1);
        if (emotionTexts.contains(factText)) {
            Bitmap bitmap = getDrawableByName(ctx, key);
            ImageSpan image = new ImageSpan(ctx, bitmap);
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }//ww  w. j  ava2  s. c  o m
    }
    return spannableString;
}

From source file:Main.java

public static CharSequence replace(Context ctx, String text) {
    if (text == null) {
        return null;
    }/*from w  w  w.j  a v  a2  s.c  o  m*/
    SpannableString spannableString = new SpannableString(text);
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String factText = matcher.group();
        String key = factText.substring(1);
        if (emotionTexts.contains(factText)) {
            Bitmap bitmap = getDrawableByName(ctx, key);
            ImageSpan image = new ImageSpan(ctx, bitmap);
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return spannableString;
}

From source file:Main.java

public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Map.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;/*  w  w  w .  ja v a2  s . c  om*/
                    break;
                }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

From source file:Main.java

public static boolean addEmoticons(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;/*from w ww.  j  a  va  2 s. c  om*/
                    break;
                }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

From source file:Main.java

/**
 * toSpannableString/*  ww w.ja  v  a  2  s .c  o m*/
 * @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("");
    }
}

From source file:ca.rmen.android.poetassistant.compat.VectorCompat.java

@SuppressWarnings("SameParameterValue")
public static ImageSpan createVectorImageSpan(Context context, @DrawableRes int resId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return new ImageSpan(context, resId);
    }//  w w w  .  j  av a2  s  .co m
    VectorDrawableCompat drawable = createVectorDrawable(context, resId);
    assert drawable != null;
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    return new ImageSpan(drawable);
}

From source file:Main.java

public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Map.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;/*from  w w w . j a  va 2  s .com*/
                    break;
                }
            }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

From source file:Main.java

public static CharSequence replace(Context context, String text) {
    if (TextUtils.isEmpty(text)) {
        return text;
    }//from  www .  java2  s. c o  m
    SpannableString spannableString = new SpannableString(text);
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String factText = matcher.group();
        String key = factText.substring(1, factText.length() - 1);
        if (contain(emojiCodes, factText)) {
            Bitmap bitmap = getEmojiDrawable(context, key);
            ImageSpan image = new ImageSpan(context, bitmap);
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return spannableString;
}