本文整理汇总了Java中android.text.SpannableStringBuilder.subSequence方法的典型用法代码示例。如果您正苦于以下问题:Java SpannableStringBuilder.subSequence方法的具体用法?Java SpannableStringBuilder.subSequence怎么用?Java SpannableStringBuilder.subSequence使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.SpannableStringBuilder
的用法示例。
在下文中一共展示了SpannableStringBuilder.subSequence方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: removeNewlines
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private static SpannableStringBuilder removeNewlines(SpannableStringBuilder s) {
int start = 0;
int end = s.length();
while (start < end && Character.isWhitespace(s.charAt(start))) {
start++;
}
while (end > start && Character.isWhitespace(s.charAt(end - 1))) {
end--;
}
return (SpannableStringBuilder) s.subSequence(start, end);
}
示例2: addEmojis
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* Convert emoji characters of the given Spannable to the according emojicon.
*
* @param context
* @param text
* @param emojiSize
* @param index
* @param length
* @param useSystemDefault
*/
public static SpannableStringBuilder addEmojis(Context context, SpannableStringBuilder text, int emojiSize, int textSize, int index, int length, boolean useSystemDefault) {
if (useSystemDefault) {
return text;
}
int textLengthToProcess = calculateLegalTextLength(text, index, length);
// remove spans throughout all text
EmojiconSpan[] oldSpans = text.getSpans(0, text.length(), EmojiconSpan.class);
for (int i = 0; i < oldSpans.length; i++) {
text.removeSpan(oldSpans[i]);
}
int[] results = new int[3];
String textStr = text.toString();
int processIdx = index;
while (processIdx < textLengthToProcess) {
boolean isEmoji = findEmoji(textStr, processIdx, textLengthToProcess, results);
int skip = results[1];
if (isEmoji) {
int icon = results[0];
boolean isQQFace = results[2] > 0;
EmojiconSpan span = new EmojiconSpan(context, icon, (int)(emojiSize * EMOJIICON_SCALE),
(int)(emojiSize * EMOJIICON_SCALE));
span.setTranslateY(isQQFace ? QQFACE_TRANSLATE_Y : EMOJIICON_TRANSLATE_Y);
if (span.getCachedDrawable() == null) {
text.replace(processIdx, processIdx + skip, "..");
//重新计算字符串的合法长度
textLengthToProcess = calculateLegalTextLength(text, index, length);
} else {
text.setSpan(span, processIdx, processIdx + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
processIdx += skip;
}
return (SpannableStringBuilder) text.subSequence(index, processIdx);
}
示例3: setEmoteSpans
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private void setEmoteSpans(SpannableStringBuilder builder) {
for (URLSpan span : builder.getSpans(0, builder.length(), URLSpan.class)) {
setLargeLinks(builder, span);
File emoteDir = new File(Environment.getExternalStorageDirectory(), "RedditEmotes");
File emoteFile = new File(emoteDir, span.getURL().replace("/", "").replaceAll("-.*", "")
+ ".png"); //BPM uses "-" to add dynamics for emotes in browser. Fall back to original here if exists.
boolean startsWithSlash = span.getURL().startsWith("/");
boolean hasOnlyOneSlash = StringUtils.countMatches(span.getURL(), "/") == 1;
if (emoteDir.exists() && startsWithSlash && hasOnlyOneSlash && emoteFile.exists()) {
//We've got an emote match
int start = builder.getSpanStart(span);
int end = builder.getSpanEnd(span);
CharSequence textCovers = builder.subSequence(start, end);
//Make sure bitmap loaded works well with screen density.
BitmapFactory.Options options = new BitmapFactory.Options();
DisplayMetrics metrics = new DisplayMetrics();
((WindowManager) getContext().getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
options.inDensity = 240;
options.inScreenDensity = metrics.densityDpi;
options.inScaled = true;
//Since emotes are not directly attached to included text, add extra character to attach image to.
builder.removeSpan(span);
if (builder.subSequence(start, end).charAt(0) != '.') {
builder.insert(start, ".");
}
Bitmap emoteBitmap = BitmapFactory.decodeFile(emoteFile.getAbsolutePath(), options);
builder.setSpan(new ImageSpan(getContext(), emoteBitmap), start, start + 1,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//Check if url span has length. If it does, it's a spoiler/caption
if (textCovers.length() > 1) {
builder.setSpan(new URLSpan("/sp"), start + 1, end + 1,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
builder.setSpan(new StyleSpan(Typeface.ITALIC), start + 1, end + 1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
builder.append("\n"); //Newline to fix text wrapping issues
}
}
}
示例4: addEmojis
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* Convert emoji characters of the given Spannable to the according emojicon.
*
* @param context
* @param text
* @param emojiSize
* @param index
* @param length
* @param useSystemDefault
*/
public static SpannableStringBuilder addEmojis(Context context, SpannableStringBuilder text, int emojiSize, int textSize, int index, int length, boolean useSystemDefault) {
if (useSystemDefault) {
return text;
}
int textLengthToProcess = calculateLegalTextLength(text, index, length);
// remove spans throughout all text
EmojiconSpan[] oldSpans = text.getSpans(0, text.length(), EmojiconSpan.class);
for (EmojiconSpan oldSpan : oldSpans) {
text.removeSpan(oldSpan);
}
int[] results = new int[3];
String textStr = text.toString();
int processIdx = index;
while (processIdx < textLengthToProcess) {
boolean isEmoji = findEmoji(textStr, processIdx, textLengthToProcess, results);
int skip = results[1];
if (isEmoji) {
int icon = results[0];
boolean isQQFace = results[2] > 0;
EmojiconSpan span = new EmojiconSpan(context, icon, (int) (emojiSize * EMOJIICON_SCALE),
(int) (emojiSize * EMOJIICON_SCALE));
span.setTranslateY(isQQFace ? QQFACE_TRANSLATE_Y : EMOJIICON_TRANSLATE_Y);
if (span.getCachedDrawable() == null) {
text.replace(processIdx, processIdx + skip, "..");
//重新计算字符串的合法长度
textLengthToProcess = calculateLegalTextLength(text, index, length);
} else {
text.setSpan(span, processIdx, processIdx + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
processIdx += skip;
}
return (SpannableStringBuilder) text.subSequence(index, processIdx);
}