Search in sources :

Example 21 with BackgroundColorSpan

use of android.text.style.BackgroundColorSpan in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class InputLogic method setComposingTextInternalWithBackgroundColor.

/**
     * Equivalent to {@link #setComposingTextInternal(CharSequence, int)} except that this method
     * allows to set {@link BackgroundColorSpan} to the composing text with the given color.
     *
     * <p>TODO: Currently the background color is exclusive with the black underline, which is
     * automatically added by the framework. We need to change the framework if we need to have both
     * of them at the same time.</p>
     * <p>TODO: Should we move this method to {@link RichInputConnection}?</p>
     *
     * @param newComposingText the composing text to be set
     * @param newCursorPosition the new cursor position
     * @param backgroundColor the background color to be set to the composing text. Set
     * {@link Color#TRANSPARENT} to disable the background color.
     * @param coloredTextLength the length of text, in Java chars, which should be rendered with
     * the given background color.
     */
private void setComposingTextInternalWithBackgroundColor(final CharSequence newComposingText, final int newCursorPosition, final int backgroundColor, final int coloredTextLength) {
    final CharSequence composingTextToBeSet;
    if (backgroundColor == Color.TRANSPARENT) {
        composingTextToBeSet = newComposingText;
    } else {
        final SpannableString spannable = new SpannableString(newComposingText);
        final BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(backgroundColor);
        final int spanLength = Math.min(coloredTextLength, spannable.length());
        spannable.setSpan(backgroundColorSpan, 0, spanLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
        composingTextToBeSet = spannable;
    }
    mConnection.setComposingText(composingTextToBeSet, newCursorPosition);
}
Also used : SpannableString(android.text.SpannableString) BackgroundColorSpan(android.text.style.BackgroundColorSpan)

Example 22 with BackgroundColorSpan

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

the class HtmlToSpannedConverter method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    Application application = ActivityThread.currentApplication();
                    sizeDip /= application.getResources().getDisplayMetrics().density;
                }
                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }
        withinStyle(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) Application(android.app.Application) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 23 with BackgroundColorSpan

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

the class HtmlToSpannedConverter method endCssStyle.

private static void endCssStyle(Editable text) {
    Strikethrough s = getLast(text, Strikethrough.class);
    if (s != null) {
        setSpanFromMark(text, s, new StrikethroughSpan());
    }
    Background b = getLast(text, Background.class);
    if (b != null) {
        setSpanFromMark(text, b, new BackgroundColorSpan(b.mBackgroundColor));
    }
    Foreground f = getLast(text, Foreground.class);
    if (f != null) {
        setSpanFromMark(text, f, new ForegroundColorSpan(f.mForegroundColor));
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) BackgroundColorSpan(android.text.style.BackgroundColorSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 24 with BackgroundColorSpan

use of android.text.style.BackgroundColorSpan in project DanmakuFlameMaster by Bilibili.

the class MainActivity 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 25 with BackgroundColorSpan

use of android.text.style.BackgroundColorSpan 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)

Aggregations

BackgroundColorSpan (android.text.style.BackgroundColorSpan)30 ForegroundColorSpan (android.text.style.ForegroundColorSpan)18 ImageSpan (android.text.style.ImageSpan)14 StrikethroughSpan (android.text.style.StrikethroughSpan)11 UnderlineSpan (android.text.style.UnderlineSpan)11 SpannableStringBuilder (android.text.SpannableStringBuilder)10 TextView (android.widget.TextView)10 SpannableString (android.text.SpannableString)7 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)6 RelativeSizeSpan (android.text.style.RelativeSizeSpan)6 StyleSpan (android.text.style.StyleSpan)6 Button (android.widget.Button)6 LinearLayout (android.widget.LinearLayout)6 Application (android.app.Application)5 CharacterStyle (android.text.style.CharacterStyle)5 SubscriptSpan (android.text.style.SubscriptSpan)5 SuggestionSpan (android.text.style.SuggestionSpan)5 SuperscriptSpan (android.text.style.SuperscriptSpan)5 TypefaceSpan (android.text.style.TypefaceSpan)5 URLSpan (android.text.style.URLSpan)5