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;
}
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;
}
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);
}
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;
}
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();
}
}
}
Aggregations