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:midian.baselib.widget.PagerSlidingTabStrip.java

private void addRightIcon(TextView tab, int i, boolean isUp) {
    String text = pager.getAdapter().getPageTitle(i).toString() + "    ";
    SpannableStringBuilder str = new SpannableStringBuilder(text + "[icon]");

    // ?Drawable?
    Drawable d = getResources().getDrawable(isUp ? R.drawable.icon_tap_up : R.drawable.icon_tap_down);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    // ImageSpan//from w ww .  ja  va2  s  . com
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    str.setSpan(span, text.length(), text.length() + "[icon]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    tab.setText(str);
}

From source file:org.catnut.util.CatnutUtils.java

/**
 * ?//from  w  w w.ja v a2  s . c  om
 *
 * @param boundPx the icon' s rectangle bound, if zero, use the default
 */
public static SpannableString text2Emotion(Context context, String key, int boundPx) {
    SpannableString spannable = new SpannableString(key);
    InputStream inputStream = null;
    Drawable drawable = null;
    try {
        inputStream = context.getAssets().open(TweetImageSpan.EMOTIONS_DIR + TweetImageSpan.EMOTIONS.get(key));
        drawable = Drawable.createFromStream(inputStream, null);
    } catch (IOException e) {
        Log.e(TAG, "load emotion error!", e);
    } finally {
        closeIO(inputStream);
    }
    if (drawable != null) {
        if (boundPx == 0) {
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        } else {
            drawable.setBounds(0, 0, boundPx, boundPx);
        }
        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
        spannable.setSpan(span, 0, key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    }
    return spannable;
}

From source file:es.usc.citius.servando.calendula.activities.CalendarActivity.java

public CharSequence addPickupList(CharSequence msg, List<PickupInfo> pks) {

    Paint textPaint = new Paint();
    //obviously, we have to set textSize into Paint object
    textPaint.setTextSize(getResources().getDimensionPixelOffset(R.dimen.medium_font_size));
    Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();

    for (PickupInfo p : pks) {
        Patient patient = pickupUtils.getPatient(p);
        int color = patient.color();
        String str = "       " + p.medicine().name() + " (" + dtf2.format(p.from().toDate()) + " - "
                + dtf2.format(p.to().toDate()) + ")\n";
        Spannable text = new SpannableString(str);
        text.setSpan(new ForegroundColorSpan(color), 0, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        Drawable d = getResources().getDrawable(AvatarMgr.res(patient.avatar()));
        d.setBounds(0, 0, fontMetrics.bottom, fontMetrics.bottom);
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        text.setSpan(span, 0, 5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        msg = TextUtils.concat(msg, text);
    }//from  w w  w  .  j a  v a2s. c  om
    return msg;
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void startImg(SpannableStringBuilder text, Attributes attributes, Html.ImageGetter img) {
    String src = attributes.getValue("", "src");
    Drawable d = null;/*from   ww w  .  j a v  a2 s  . c  o  m*/

    if (img != null) {
        d = img.getDrawable(src);
    }

    if (d == null) {
        // don't draw anything
        return;
    }

    int len = text.length();
    text.append("\uFFFC");

    text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:com.marshalchen.common.uimodule.modifysys.PagerTitleStrip.java

SpannableString changeSpanString(String temp, int position, int color) {

    try {//from www.ja  va  2s .  co m
        temp = temp.replaceAll("-", " - ");

        SpannableString ss = new SpannableString(temp);
        Matrix matrix = new Matrix();

        matrix.postRotate(180);

        Bitmap bitmaporg = BitmapFactory.decodeResource(getResources(), R.drawable.ico_arrow2x);
        if (color == 2) {
            bitmaporg = BitmapFactory.decodeResource(getResources(), R.drawable.ico_arrow_grey2x);
        }
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmaporg, 0, 0, bitmaporg.getWidth(), bitmaporg.getHeight(),
                matrix, true);
        BitmapDrawable bmd = new BitmapDrawable(getResources(), resizedBitmap);
        BitmapDrawable bm = new BitmapDrawable(getResources(), bitmaporg);
        Drawable drawable = position == 0 ? bm : bmd;
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * 12 / 10,
                drawable.getIntrinsicHeight() * 12 / 10);
        ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);

        ss.setSpan(imageSpan, temp.indexOf("-"), temp.indexOf("-") + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
        return null;
    }
}

From source file:jp.gr.java_conf.piropiroping.bluetoothcommander.MainActivity.java

private void convertBitmapCommand(String command, int start, int stop) {
    //???//from www  .  j  av a  2s .  co  m
    Paint objPaint = new Paint();
    Bitmap objBitmap;
    Canvas objCanvas;
    int textSize = 40;
    int textWidth = textSize * command.length(), textHeight = textSize;

    objPaint.setAntiAlias(true);
    objPaint.setColor(Color.BLUE);
    objPaint.setTextSize(textSize);
    Paint.FontMetrics fm = objPaint.getFontMetrics();
    objPaint.getTextBounds(command, 0, command.length(), new Rect(0, 0, textWidth, textHeight));

    //?
    textWidth = (int) objPaint.measureText(command);
    textHeight = (int) (Math.abs(fm.top) + fm.bottom);
    objBitmap = Bitmap.createBitmap(textWidth, textHeight, Bitmap.Config.ARGB_8888);

    //???
    objCanvas = new Canvas(objBitmap);
    objCanvas.drawText(command, 0, Math.abs(fm.top), objPaint);

    Editable editable = mOutEditText.getText();
    SpannableStringBuilder ssb = (SpannableStringBuilder) editable;
    ImageSpan is = new ImageSpan(getApplicationContext(), objBitmap);
    ssb.setSpan(is, start, stop + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:com.ruesga.rview.widget.TagEditTextView.java

private void createChip(Editable s, boolean nextIsTag) {
    int start = mTagList.size();
    int end = s.length() + (nextIsTag ? -1 : 0);
    String tagText = s.subSequence(start, end).toString().trim();
    tagText = NON_UNICODE_CHAR_PATTERN.matcher(tagText).replaceAll("");
    if (tagText.isEmpty() || tagText.length() <= 1) {
        // User is still writing
        return;/*ww w.  ja va  2s .c  o  m*/
    }
    String charText = tagText.substring(0, 1);
    if (!VALID_TAGS.contains(charText) || (charText.charAt(0) == VALID_TAGS.charAt(1) && !mSupportsUserTags)) {
        char tag = mDefaultTagMode == TAG_MODE.HASH ? VALID_TAGS.charAt(0) : VALID_TAGS.charAt(1);
        tagText = tag + tagText;
    }

    // Replace the new tag
    s.replace(start, end, CHIP_REPLACEMENT_CHAR);

    // Create the tag and its spannable
    final Tag tag = new Tag();
    tag.mTag = NON_UNICODE_CHAR_PATTERN.matcher(tagText).replaceAll("");
    Bitmap b = createTagChip(tag);
    ImageSpan span = new ImageSpan(getContext(), b);
    s.setSpan(span, start, start + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tag.w = b.getWidth();
    tag.h = b.getHeight();
    mTagList.add(tag);

    notifyTagCreated(tag);
}

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static void startImg(SpannableStringBuilder text, Attributes attributes, HtmlParser.ImageGetter img) {
    String src = attributes.getValue("", "src");
    Drawable d = null;//from  w  w w  . j  a  v  a 2  s. c om

    if (img != null) {
        d = img.getDrawable(src);
    }

    if (d == null) {
        d = ResourcesCompat.getDrawable(Resources.getSystem(), android.R.drawable.ic_menu_report_image, null);
        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);
}

From source file:info.guardianproject.otr.app.im.app.MessageView.java

private SpannableString formatTimeStamp(Date date, int messageType, DateFormat format,
        MessageView.DeliveryState delivery, EncryptionState encryptionState) {

    StringBuilder deliveryText = new StringBuilder();
    deliveryText.append(format.format(date));
    deliveryText.append(' ');

    if (delivery != null) {
        //this is for delivery
        if (delivery == DeliveryState.DELIVERED) {

            deliveryText.append(DELIVERED_SUCCESS);

        } else if (delivery == DeliveryState.UNDELIVERED) {

            deliveryText.append(DELIVERED_FAIL);

        }// w w w  . j a  v  a2s  . c o  m

    }

    if (messageType != Imps.MessageType.POSTPONED)
        deliveryText.append(DELIVERED_SUCCESS);//this is for sent, so we know show 2 checks like WhatsApp!

    SpannableString spanText = null;

    if (encryptionState == EncryptionState.ENCRYPTED) {
        deliveryText.append('X');
        spanText = new SpannableString(deliveryText.toString());
        int len = spanText.length();

        spanText.setSpan(new ImageSpan(getContext(), R.drawable.lock16), len - 1, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    } else if (encryptionState == EncryptionState.ENCRYPTED_AND_VERIFIED) {
        deliveryText.append('X');
        spanText = new SpannableString(deliveryText.toString());
        int len = spanText.length();

        spanText.setSpan(new ImageSpan(getContext(), R.drawable.lock16), len - 1, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    } else {
        spanText = new SpannableString(deliveryText.toString());
        int len = spanText.length();

    }

    //   spanText.setSpan(new StyleSpan(Typeface.SANS_SERIF), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // spanText.setSpan(new RelativeSizeSpan(0.8f), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    //    spanText.setSpan(new ForegroundColorSpan(R.color.soft_grey),
    //        0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spanText;
}

From source file:org.awesomeapp.messenger.ui.MessageListItem.java

private SpannableString formatTimeStamp(Date date, int messageType, MessageListItem.DeliveryState delivery,
        EncryptionState encryptionState, String nickname) {

    StringBuilder deliveryText = new StringBuilder();

    if (nickname != null) {
        deliveryText.append(nickname);/*from w w w .j a  v  a 2s  . c o  m*/
        deliveryText.append(' ');
    }

    deliveryText.append(sPrettyTime.format(date));

    SpannableString spanText = null;

    spanText = new SpannableString(deliveryText.toString());

    if (delivery != null) {
        deliveryText.append(' ');
        //this is for delivery

        if (messageType == Imps.MessageType.POSTPONED) {
            //do nothing
            deliveryText.append("X");
            spanText = new SpannableString(deliveryText.toString());
            int len = spanText.length();
            spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_message_wait_grey), len - 1, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (delivery == DeliveryState.DELIVERED) {

            if (encryptionState == EncryptionState.ENCRYPTED
                    || encryptionState == EncryptionState.ENCRYPTED_AND_VERIFIED) {
                deliveryText.append("XX");
                spanText = new SpannableString(deliveryText.toString());
                int len = spanText.length();

                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_delivered_grey), len - 2, len - 1,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_encrypted_grey), len - 1, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            } else {
                deliveryText.append("X");
                spanText = new SpannableString(deliveryText.toString());
                int len = spanText.length();
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_delivered_grey), len - 1, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }

        } else if (delivery == DeliveryState.UNDELIVERED) {

            if (encryptionState == EncryptionState.ENCRYPTED
                    || encryptionState == EncryptionState.ENCRYPTED_AND_VERIFIED) {
                deliveryText.append("XX");
                spanText = new SpannableString(deliveryText.toString());
                int len = spanText.length();
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_sent_grey), len - 2, len - 1,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_encrypted_grey), len - 1, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                deliveryText.append("XX");
                spanText = new SpannableString(deliveryText.toString());
                int len = spanText.length();
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_sent_grey), len - 1, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }

        } else if (delivery == DeliveryState.NEUTRAL) {

            if (encryptionState == EncryptionState.ENCRYPTED
                    || encryptionState == EncryptionState.ENCRYPTED_AND_VERIFIED) {
                deliveryText.append("XX");
                spanText = new SpannableString(deliveryText.toString());
                int len = spanText.length();
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_sent_grey), len - 2, len - 1,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_encrypted_grey), len - 1, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                deliveryText.append("X");
                spanText = new SpannableString(deliveryText.toString());
                int len = spanText.length();
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_sent_grey), len - 1, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }

        }

    } else {
        if (encryptionState == EncryptionState.ENCRYPTED
                || encryptionState == EncryptionState.ENCRYPTED_AND_VERIFIED) {
            deliveryText.append('X');
            spanText = new SpannableString(deliveryText.toString());
            int len = spanText.length();

            if (encryptionState == EncryptionState.ENCRYPTED
                    || encryptionState == EncryptionState.ENCRYPTED_AND_VERIFIED)
                spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_encrypted_grey), len - 1, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (messageType == Imps.MessageType.OUTGOING) {
            //do nothing
            deliveryText.append("X");
            spanText = new SpannableString(deliveryText.toString());
            int len = spanText.length();
            spanText.setSpan(new ImageSpan(getContext(), R.drawable.ic_message_wait_grey), len - 1, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    return spanText;
}