use of android.text.style.URLSpan in project keleFanfou by kelefun.
the class StatusAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
ItemViewHolder itemHolder = (ItemViewHolder) holder;
// 把每个图片视图设置不同的Transition名称, 防止在一个视图内有多个相同的名称, 在变换的时候造成混乱
// Fragment支持多个View进行变换, 使用适配器时, 需要加以区分
ViewCompat.setTransitionName(itemHolder.screenNameView, String.valueOf(position) + "_screenNameView");
ViewCompat.setTransitionName(itemHolder.timeSourceView, String.valueOf(position) + "_timeSourceView");
ViewCompat.setTransitionName(itemHolder.statusView, String.valueOf(position) + "_statusView");
ViewCompat.setTransitionName(itemHolder.avatarView, String.valueOf(position) + "_avatarView");
ViewCompat.setTransitionName(itemHolder.photoView, String.valueOf(position) + "_photoView");
if (data.size() != 0) {
Status status = data.get(position);
itemHolder.screenNameView.setText(status.getUser().getScreenName());
itemHolder.timeSourceView.setText(DateUtil.toAgo(status.getCreatedAt()) + Html.fromHtml(status.getSource()).toString());
// 处理文本点击跳转
// 格式化<a herf ,mobile等标签
Spannable statusSpan = (Spannable) Html.fromHtml(status.getText());
CharSequence text = statusSpan.toString();
URLSpan[] urls = statusSpan.getSpans(0, text.length(), URLSpan.class);
SpannableStringBuilder activitySpan = new SpannableStringBuilder(statusSpan);
activitySpan.clearSpans();
for (URLSpan url : urls) {
ActivitySpan myURLSpan = new ActivitySpan(url.getURL());
activitySpan.setSpan(myURLSpan, statusSpan.getSpanStart(url), statusSpan.getSpanEnd(url), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
itemHolder.statusView.setText(activitySpan);
// 使标签可点击
itemHolder.statusView.setMovementMethod(LinkMovementMethod.getInstance());
// 图片加载
Glide.with(mContext).load(status.getUser().getProfileImageUrl()).into(itemHolder.avatarView);
if (status.getPhoto() != null) {
itemHolder.photoView.setVisibility(View.VISIBLE);
if (status.getPhoto().getLargeurl().endsWith("gif")) {
// 动图标签
itemHolder.photoView.setTagEnable(true);
} else {
itemHolder.photoView.setTagEnable(false);
}
Glide.with(mContext).load(status.getPhoto().getImageurl()).into(itemHolder.photoView);
} else {
itemHolder.photoView.setVisibility(View.GONE);
}
}
}
}
use of android.text.style.URLSpan in project android_frameworks_base by DirtyUnicorns.
the class Editor method selectCurrentWord.
/**
* Adjusts selection to the word under last touch offset. Return true if the operation was
* successfully performed.
*/
private boolean selectCurrentWord() {
if (!mTextView.canSelectText()) {
return false;
}
if (needsToSelectAllToSelectWordOrParagraph()) {
return mTextView.selectAllText();
}
long lastTouchOffsets = getLastTouchOffsets();
final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets);
final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets);
// Safety check in case standard touch event handling has been bypassed
if (minOffset < 0 || minOffset > mTextView.getText().length())
return false;
if (maxOffset < 0 || maxOffset > mTextView.getText().length())
return false;
int selectionStart, selectionEnd;
// If a URLSpan (web address, email, phone...) is found at that position, select it.
URLSpan[] urlSpans = ((Spanned) mTextView.getText()).getSpans(minOffset, maxOffset, URLSpan.class);
if (urlSpans.length >= 1) {
URLSpan urlSpan = urlSpans[0];
selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan);
selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan);
} else {
// FIXME - We should check if there's a LocaleSpan in the text, this may be
// something we should try handling or checking for.
final WordIterator wordIterator = getWordIterator();
wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset);
selectionStart = wordIterator.getBeginning(minOffset);
selectionEnd = wordIterator.getEnd(maxOffset);
if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE || selectionStart == selectionEnd) {
// Possible when the word iterator does not properly handle the text's language
long range = getCharClusterRange(minOffset);
selectionStart = TextUtils.unpackRangeStartFromLong(range);
selectionEnd = TextUtils.unpackRangeEndFromLong(range);
}
}
Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
return selectionEnd > selectionStart;
}
use of android.text.style.URLSpan in project Slide by ccrama.
the class SpoilerRobotoTextView method setEmoteSpans.
private void setEmoteSpans(SpannableStringBuilder builder) {
for (URLSpan span : builder.getSpans(0, builder.length(), URLSpan.class)) {
if (SettingValues.typeInText) {
setLinkTypes(builder, span);
}
if (SettingValues.largeLinks) {
setLargeLinks(builder, span);
}
File emoteDir = new File(Environment.getExternalStorageDirectory(), "RedditEmotes");
File emoteFile = new File(emoteDir, span.getURL().replace("/", "").replaceAll("-.*", "") + // BPM uses "-" to add dynamics for emotes in browser. Fall back to original here if exists.
".png");
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);
}
// Newline to fix text wrapping issues
builder.append("\n");
}
}
}
use of android.text.style.URLSpan in project Slide by ccrama.
the class SpoilerRobotoTextView method setSpoilerStyle.
/**
* Set the necessary spans for each spoiler. <p/> The algorithm works in the same way as
* <code>setCodeFont</code>.
*
* @param sequence
* @return
*/
private CharSequence setSpoilerStyle(SpannableStringBuilder sequence, String subreddit) {
int start = 0;
int end = 0;
for (int i = 0; i < sequence.length(); i++) {
if (sequence.charAt(i) == '[' && i < sequence.length() - 3) {
if (sequence.charAt(i + 1) == '[' && sequence.charAt(i + 2) == 's' && sequence.charAt(i + 3) == '[') {
start = i;
}
} else if (sequence.charAt(i) == ']' && i < sequence.length() - 3) {
if (sequence.charAt(i + 1) == 's' && sequence.charAt(i + 2) == ']' && sequence.charAt(i + 3) == ']') {
end = i;
}
}
if (end > start) {
sequence.delete(end, end + 4);
sequence.delete(start, start + 4);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Palette.getDarkerColor(Palette.getColor(subreddit)));
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Palette.getDarkerColor(Palette.getColor(subreddit)));
ForegroundColorSpan underneathColorSpan = new ForegroundColorSpan(Color.WHITE);
URLSpan urlSpan = sequence.getSpans(start, start, URLSpan.class)[0];
sequence.setSpan(urlSpan, sequence.getSpanStart(urlSpan), start - 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
sequence.setSpan(new URLSpanNoUnderline("#spoilerhidden"), start, end - 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// spoiler text has a space at the front
sequence.setSpan(backgroundColorSpan, start + 1, end - 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sequence.setSpan(underneathColorSpan, start, end - 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sequence.setSpan(foregroundColorSpan, start, end - 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
storedSpoilerSpans.add(underneathColorSpan);
storedSpoilerSpans.add(foregroundColorSpan);
storedSpoilerSpans.add(backgroundColorSpan);
// Shift 1 to account for remove of beginning "<"
storedSpoilerStarts.add(start - 1);
storedSpoilerStarts.add(start - 1);
storedSpoilerStarts.add(start - 1);
storedSpoilerEnds.add(end - 5);
storedSpoilerEnds.add(end - 5);
storedSpoilerEnds.add(end - 5);
// remove the trailing <
sequence.delete(start - 2, start - 1);
start = 0;
end = 0;
// move back to compensate for removal of [[s[
i = i - 5;
}
}
return sequence;
}
use of android.text.style.URLSpan in project sbt-android by scala-android.
the class HtmlUtils method parseHtml.
/**
* Parse the given input using {@link TouchableUrlSpan}s
* rather than vanilla {@link android.text.style.URLSpan}s so that they respond to touch.
*
* @param input
* @param linkTextColor
* @param linkHighlightColor
* @return
*/
public static Spanned parseHtml(String input, ColorStateList linkTextColor, int linkHighlightColor) {
SpannableStringBuilder spanned = (SpannableStringBuilder) Html.fromHtml(input);
// strip any trailing newlines
while (spanned.charAt(spanned.length() - 1) == '\n') {
spanned = spanned.delete(spanned.length() - 1, spanned.length());
}
URLSpan[] urlSpans = spanned.getSpans(0, spanned.length(), URLSpan.class);
for (URLSpan urlSpan : urlSpans) {
int start = spanned.getSpanStart(urlSpan);
int end = spanned.getSpanEnd(urlSpan);
spanned.removeSpan(urlSpan);
// spanned.subSequence(start, start + 1) == "@" TODO send to our own user activity...
// when i've written it
spanned.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spanned;
}
Aggregations