use of android.text.style.ImageSpan in project XobotOS by xamarin.
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 RichText by zzhoujay.
the class CachedSpannedParser method handleImage.
private int handleImage(SpannableStringBuilder ssb, ImageGetterWrapper imageGetterWrapper, RichTextConfig config, boolean cached) {
if (cached) {
ClickableImageSpan[] cis = ssb.getSpans(0, ssb.length(), ClickableImageSpan.class);
if (cis != null && cis.length > 0) {
for (ClickableImageSpan ci : cis) {
int start = ssb.getSpanStart(ci);
int end = ssb.getSpanEnd(ci);
ssb.removeSpan(ci);
OnImageClickListener onImageClickListener = null;
OnImageLongClickListener onImageLongClickListener = null;
if (config.clickable > 0) {
onImageClickListener = config.onImageClickListener;
onImageLongClickListener = config.onImageLongClickListener;
}
Drawable drawable = imageGetterWrapper.getDrawable(ci.getSource());
if (drawable == null) {
drawable = new ColorDrawable(Color.TRANSPARENT);
}
ClickableImageSpan nci = new ClickableImageSpan(drawable, ci, onImageClickListener, onImageLongClickListener);
ssb.setSpan(nci, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return cis.length;
}
} else if (!config.noImage) {
ImageSpan[] iss = ssb.getSpans(0, ssb.length(), ImageSpan.class);
if (iss != null && iss.length > 0) {
ArrayList<String> imageUrls = new ArrayList<>(iss.length);
for (int i = 0; i < iss.length; i++) {
ImageSpan imageSpan = iss[i];
String imageUrl = imageSpan.getSource();
imageUrls.add(imageUrl);
int start = ssb.getSpanStart(imageSpan);
int end = ssb.getSpanEnd(imageSpan);
ClickableSpan[] clickableSpans = ssb.getSpans(start, end, ClickableSpan.class);
if (clickableSpans != null && clickableSpans.length != 0) {
for (ClickableSpan cs : clickableSpans) {
ssb.removeSpan(cs);
}
}
OnImageClickListener onImageClickListener = null;
OnImageLongClickListener onImageLongClickListener = null;
if (config.clickable > 0) {
onImageClickListener = config.onImageClickListener;
onImageLongClickListener = config.onImageLongClickListener;
}
Drawable drawable = imageGetterWrapper.getDrawable(imageUrl);
if (drawable == null) {
drawable = new ColorDrawable(Color.TRANSPARENT);
}
ClickableImageSpan cacheImageSpan = new ClickableImageSpan(drawable, imageUrls, i, onImageClickListener, onImageLongClickListener);
ssb.removeSpan(imageSpan);
ssb.setSpan(cacheImageSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return iss.length;
}
}
return 0;
}
use of android.text.style.ImageSpan in project android_frameworks_base by DirtyUnicorns.
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 wechat by motianhuo.
the class SmileUtils method addSmiles.
/**
* replace existing spannable with smiles
*
* @param context
* @param spannable
* @return
*/
public static boolean addSmiles(Context context, Spannable spannable) {
boolean hasChanges = false;
for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
Matcher matcher = entry.getKey().matcher(spannable);
while (matcher.find()) {
boolean set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) if (spannable.getSpanStart(span) >= matcher.start() && spannable.getSpanEnd(span) <= matcher.end())
spannable.removeSpan(span);
else {
set = false;
break;
}
if (set) {
hasChanges = true;
Drawable drawable = context.getResources().getDrawable(entry.getValue());
// 这里设置图片的大小
drawable.setBounds(0, 0, 50, 50);
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
spannable.setSpan(imageSpan, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return hasChanges;
}
use of android.text.style.ImageSpan in project android_frameworks_base by AOSPA.
the class RecentsCreateFragment method appendDrawable.
private static void appendDrawable(SpannableStringBuilder b, Drawable d) {
final int length = b.length();
b.append("〉");
b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Aggregations