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 Drawable drawable) 

Source Link

Document

Constructs an ImageSpan from a drawable with the default alignment DynamicDrawableSpan#ALIGN_BOTTOM .

Usage

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {/* w w w .  j  ava  2s .  c  o m*/
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
    }

    return ss;
}

From source file:Main.java

public static SpannableString setTextImg(String content, int startIndex, int endIndex, Drawable drawable) {
    if (TextUtils.isEmpty(content) || startIndex < 0 || endIndex >= content.length()
            || startIndex >= endIndex) {
        return null;
    }//ww w . ja v  a 2s . com

    SpannableString spannableString = new SpannableString(content);
    spannableString.setSpan(new ImageSpan(drawable), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannableString;
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {//from www  . jav a2  s .  c  om
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
        // Logger.e(TAG, ex);
    }

    return ss;
}

From source file:Main.java

public static CharSequence addIcon(CharSequence total, BitmapDrawable bitmapDrawable, int height) {
    SpannableString string = new SpannableString("  ");
    ImageSpan imageSpan = new ImageSpan(bitmapDrawable);

    int width = (int) (height
            / (bitmapDrawable.getIntrinsicHeight() / (float) bitmapDrawable.getIntrinsicWidth()));

    imageSpan.getDrawable().setBounds(0, 0, width, height);
    string.setSpan(imageSpan, 0, 1, 0);/*from  w  ww.j a v a  2s  .co  m*/
    if (total == null) {
        return string;
    } else {
        return TextUtils.concat(total, string);
    }
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {/*from w w  w.ja v  a2  s. c o m*/
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return ss;
}

From source file:Main.java

public static void setSpanBetweenTokens(SpannableString ss, String text, String token, CharacterStyle... cs) {
    // Start and end refer to the points where the span will apply
    int tokenLen = token.length();
    int base = 0;

    while (true) {
        int start = text.indexOf(token, base);
        int end = text.indexOf(token, start + tokenLen);

        if (start < 0 || end < 0) {
            break;
        }/*w  w w.j a v a  2 s . c o  m*/

        base = end + tokenLen;

        for (CharacterStyle c : cs) {
            ss.setSpan(CharacterStyle.wrap(c), start + tokenLen, end, 0);
        }

        Drawable blankDrawable = new Drawable() {

            @Override
            public void setColorFilter(ColorFilter cf) {
            }

            @Override
            public void setAlpha(int alpha) {
            }

            @Override
            public int getOpacity() {
                return 0;
            }

            @Override
            public void draw(Canvas canvas) {
            }
        };

        // because AbsoluteSizeSpan(0) doesn't work on older versions
        ss.setSpan(new ImageSpan(blankDrawable), start, start + tokenLen, 0);
        ss.setSpan(new ImageSpan(blankDrawable), end, end + tokenLen, 0);
    }
}

From source file:org.cnc.mombot.utils.ContactsEditText.java

private SpannableStringBuilder addPeopleToGroupChat(SpannableStringBuilder spannable) {
    // For tag friends.
    final Matcher matcherTag = mPatternTagFriend.matcher(spannable);
    while (matcherTag.find()) {
        // Set span. group 1 is name
        View view = createContactTextView(matcherTag.group(1));
        BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(view);
        spannable.setSpan(new ImageSpan(bd), matcherTag.start(), matcherTag.end(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }/*from ww  w  . jav a2 s.co  m*/
    return spannable;
}

From source file:cn.edu.wyu.documentviewer.RecentsCreateFragment.java

private static void appendDrawable(SpannableStringBuilder b, Drawable d) {
    final int length = b.length();
    b.append("\u232a");
    b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:app.abhijit.iter.MainActivity.java

private void setupGettingStartedHint() {
    TextView gettingStartedHint = (TextView) findViewById(R.id.hint_getting_started);
    int lineHeight = gettingStartedHint.getLineHeight();
    Drawable hamburgerIcon = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_hamburger, null);
    hamburgerIcon.setBounds(0, 0, lineHeight, lineHeight);
    SpannableStringBuilder hintText = new SpannableStringBuilder();
    hintText.append(getResources().getString(R.string.hint_getting_started_1)).append(" ");
    hintText.setSpan(new ImageSpan(hamburgerIcon), hintText.length() - 1, hintText.length(), 0);
    hintText.append(getResources().getString(R.string.hint_getting_started_2));
    gettingStartedHint.setText(hintText);
}

From source file:tv.acfun.video.CommentsActivity.java

private void initCommentsBar() {
    mCommentBar = findViewById(R.id.comments_bar);
    mBtnSend = (ImageButton) findViewById(R.id.comments_send_btn);
    mCommentText = (EditText) findViewById(R.id.comments_edit);
    mBtnEmotion = findViewById(R.id.comments_emotion_btn);
    mEmotionGrid = (GridView) findViewById(R.id.emotions);
    mBtnSend.setOnClickListener(this);
    mBtnEmotion.setOnClickListener(this);
    mEmotionGrid.setAdapter(mEmotionAdapter);
    mEmotionGrid.setOnItemClickListener(new OnItemClickListener() {

        @Override/*  w ww .ja va2 s.  co m*/
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int index = mCommentText.getSelectionEnd();
            Editable text = mCommentText.getText();
            String emotion = parent.getItemAtPosition(position).toString();
            text.insert(index, emotion);
            EmotionView v = (EmotionView) parent.getAdapter().getView(position, null, null);
            Drawable drawable = TextViewUtils.convertViewToDrawable(v);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2, drawable.getIntrinsicHeight() / 2);
            text.setSpan(new ImageSpan(drawable), index, index + emotion.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

    });
}