Search in sources :

Example 66 with ImageSpan

use of android.text.style.ImageSpan in project platform_frameworks_base by android.

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 67 with ImageSpan

use of android.text.style.ImageSpan in project GSYVideoPlayer by CarGuo.

the class DanamakuAdapter method createSpannable.

private SpannableStringBuilder createSpannable(Drawable drawable) {
    String text = "bitmap";
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
    //ImageSpan.ALIGN_BOTTOM);
    ImageSpan span = new ImageSpan(drawable);
    spannableStringBuilder.setSpan(span, 0, text.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    spannableStringBuilder.append("图文混排");
    spannableStringBuilder.setSpan(new BackgroundColorSpan(Color.parseColor("#8A2233B1")), 0, spannableStringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return spannableStringBuilder;
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan) ImageSpan(android.text.style.ImageSpan)

Example 68 with ImageSpan

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

the class ViewPropertyAlphaActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_properties);
    getWindow().getDecorView().postDelayed(new Runnable() {

        @Override
        public void run() {
            startAnim(R.id.button);
            startAnim(R.id.textview);
            startAnim(R.id.spantext);
            startAnim(R.id.edittext);
            startAnim(R.id.selectedtext);
            startAnim(R.id.textviewbackground);
            startAnim(R.id.layout);
            startAnim(R.id.imageview);
            startAnim(myViewAlphaDefault);
            startAnim(myViewAlphaHandled);
            EditText selectedText = (EditText) findViewById(R.id.selectedtext);
            selectedText.setSelection(3, 8);
        }
    }, 2000);
    Button invalidator = (Button) findViewById(R.id.invalidateButton);
    invalidator.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            findViewById(R.id.textview).invalidate();
            findViewById(R.id.spantext).invalidate();
        }
    });
    TextView textView = (TextView) findViewById(R.id.spantext);
    if (textView != null) {
        SpannableStringBuilder text = new SpannableStringBuilder("Now this is a short text message with spans");
        text.setSpan(new BackgroundColorSpan(Color.RED), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new SuggestionSpan(this, new String[] { "longer" }, 3), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new UnderlineSpan(), 17, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ImageSpan(this, R.drawable.icon), 21, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(text);
    }
    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    myViewAlphaDefault = new MyView(this, false);
    myViewAlphaDefault.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    container.addView(myViewAlphaDefault);
    myViewAlphaHandled = new MyView(this, true);
    myViewAlphaHandled.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    container.addView(myViewAlphaHandled);
}
Also used : EditText(android.widget.EditText) ForegroundColorSpan(android.text.style.ForegroundColorSpan) TextView(android.widget.TextView) View(android.view.View) UnderlineSpan(android.text.style.UnderlineSpan) Button(android.widget.Button) TextView(android.widget.TextView) SuggestionSpan(android.text.style.SuggestionSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan) LinearLayout(android.widget.LinearLayout) ImageSpan(android.text.style.ImageSpan)

Example 69 with ImageSpan

use of android.text.style.ImageSpan in project WordPress-Android by wordpress-mobile.

the class EmoticonsUtils method replaceEmoticonsWithEmoji.

public static Spanned replaceEmoticonsWithEmoji(SpannableStringBuilder html) {
    ImageSpan[] imgs = html.getSpans(0, html.length(), ImageSpan.class);
    for (ImageSpan img : imgs) {
        String emoticon = EmoticonsUtils.lookupImageSmiley(img.getSource());
        if (!emoticon.equals("")) {
            int start = html.getSpanStart(img);
            html.replace(start, html.getSpanEnd(img), emoticon);
            html.setSpan(new ForegroundColorSpan(EMOTICON_COLOR), start, start + emoticon.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            html.removeSpan(img);
        }
    }
    return html;
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) ImageSpan(android.text.style.ImageSpan)

Example 70 with ImageSpan

use of android.text.style.ImageSpan in project WordPress-Android by wordpress-mobile.

the class NotificationsUtils method addImageSpansForBlockMedia.

/**
     * Adds ImageSpans to the passed SpannableStringBuilder
     */
private static void addImageSpansForBlockMedia(TextView textView, JSONObject subject, SpannableStringBuilder spannableStringBuilder) {
    if (textView == null || subject == null || spannableStringBuilder == null)
        return;
    Context context = textView.getContext();
    JSONArray mediaArray = subject.optJSONArray("media");
    if (context == null || mediaArray == null) {
        return;
    }
    Drawable loading = context.getResources().getDrawable(org.wordpress.android.editor.R.drawable.legacy_dashicon_format_image_big_grey);
    Drawable failed = context.getResources().getDrawable(R.drawable.ic_notice_grey_500_48dp);
    // Note: notifications_max_image_size seems to be the max size an ImageSpan can handle,
    // otherwise it would load blank white
    WPImageGetter imageGetter = new WPImageGetter(textView, context.getResources().getDimensionPixelSize(R.dimen.notifications_max_image_size), WordPress.sImageLoader, loading, failed);
    int indexAdjustment = 0;
    String imagePlaceholder;
    for (int i = 0; i < mediaArray.length(); i++) {
        JSONObject mediaObject = mediaArray.optJSONObject(i);
        if (mediaObject == null) {
            continue;
        }
        final Drawable remoteDrawable = imageGetter.getDrawable(mediaObject.optString("url", ""));
        ImageSpan noteImageSpan = new ImageSpan(remoteDrawable, mediaObject.optString("url", ""));
        int startIndex = JSONUtils.queryJSON(mediaObject, "indices[0]", -1);
        int endIndex = JSONUtils.queryJSON(mediaObject, "indices[1]", -1);
        if (startIndex >= 0) {
            startIndex += indexAdjustment;
            endIndex += indexAdjustment;
            if (startIndex > spannableStringBuilder.length()) {
                continue;
            }
            // If we have a range, it means there is alt text that should be removed
            if (endIndex > startIndex && endIndex <= spannableStringBuilder.length()) {
                spannableStringBuilder.replace(startIndex, endIndex, "");
            }
            // We need an empty space to insert the ImageSpan into
            imagePlaceholder = " ";
            // Move the image to a new line if needed
            int previousCharIndex = (startIndex > 0) ? startIndex - 1 : 0;
            if (!spannableHasCharacterAtIndex(spannableStringBuilder, '\n', previousCharIndex) || spannableStringBuilder.getSpans(startIndex, startIndex, ImageSpan.class).length > 0) {
                imagePlaceholder = "\n ";
            }
            int spanIndex = startIndex + imagePlaceholder.length() - 1;
            // Add a newline after the image if needed
            if (!spannableHasCharacterAtIndex(spannableStringBuilder, '\n', startIndex) && !spannableHasCharacterAtIndex(spannableStringBuilder, '\r', startIndex)) {
                imagePlaceholder += "\n";
            }
            spannableStringBuilder.insert(startIndex, imagePlaceholder);
            // Add the image span
            spannableStringBuilder.setSpan(noteImageSpan, spanIndex, spanIndex + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // Add an AlignmentSpan to center the image
            spannableStringBuilder.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), spanIndex, spanIndex + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            indexAdjustment += imagePlaceholder.length();
        }
    }
}
Also used : Context(android.content.Context) AlignmentSpan(android.text.style.AlignmentSpan) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Drawable(android.graphics.drawable.Drawable) WPImageGetter(org.wordpress.android.util.helpers.WPImageGetter) 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