use of android.text.style.ImageSpan in project WordPress-Android by wordpress-mobile.
the class HtmlToSpannedConverter method startImg.
private void startImg(SpannableStringBuilder text, Attributes attributes, WPHtml.ImageGetter img) {
if (mContext == null)
return;
String src = attributes.getValue("android-uri");
Bitmap resizedBitmap = null;
try {
resizedBitmap = ImageUtils.getWPImageSpanThumbnailFromFilePath(mContext, src, mMaxImageWidth);
if (resizedBitmap == null && src != null) {
if (src.contains("video")) {
resizedBitmap = BitmapFactory.decodeResource(mContext.getResources(), org.wordpress.android.editor.R.drawable.media_movieclip);
} else {
resizedBitmap = BitmapFactory.decodeResource(mContext.getResources(), org.wordpress.android.R.drawable.media_image_placeholder);
}
}
} catch (OutOfMemoryError e) {
CrashlyticsUtils.logException(e, AppLog.T.UTILS);
}
if (resizedBitmap != null) {
int len = text.length();
text.append("");
Uri curStream = Uri.parse(src);
if (curStream == null) {
return;
}
WPImageSpan is = new WPImageSpan(mContext, resizedBitmap, curStream);
// Get the MediaFile data from db
MediaModel mediaModel = mMediaStore.getPostMediaWithPath(mPost.getId(), src);
MediaFile mediaFile = FluxCUtils.mediaFileFromMediaModel(mediaModel);
if (mediaFile != null) {
is.setMediaFile(mediaFile);
is.setImageSource(curStream);
text.setSpan(is, len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
AlignmentSpan.Standard as = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
text.setSpan(as, text.getSpanStart(is), text.getSpanEnd(is), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else if (mPost != null) {
if (mPost.isLocalDraft()) {
if (attributes != null) {
text.append("<img");
for (int i = 0; i < attributes.getLength(); i++) {
// Attr name
String aName = attributes.getLocalName(i);
if ("".equals(aName))
aName = attributes.getQName(i);
text.append(" ");
text.append(aName + "=\"" + attributes.getValue(i) + "\"");
}
text.append(" />\n");
}
}
} else if (src == null) {
//get regular src value from <img/> tag's src attribute
src = attributes.getValue("", "src");
Drawable d = null;
if (img != null) {
d = img.getDrawable(src);
}
if (d != null) {
int len = text.length();
text.append("");
text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
// noop - we're not showing a default image here
}
}
}
use of android.text.style.ImageSpan in project WordPress-Android by wordpress-mobile.
the class ReaderLinkMovementMethod method onTouchEvent.
@Override
public boolean onTouchEvent(@NonNull TextView textView, @NonNull Spannable buffer, @NonNull MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= textView.getTotalPaddingLeft();
y -= textView.getTotalPaddingTop();
x += textView.getScrollX();
y += textView.getScrollY();
Layout layout = textView.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ImageSpan[] images = buffer.getSpans(off, off, ImageSpan.class);
if (images != null && images.length > 0) {
EnumSet<PhotoViewerOption> options = EnumSet.noneOf(PhotoViewerOption.class);
if (mIsPrivate) {
options.add(ReaderActivityLauncher.PhotoViewerOption.IS_PRIVATE_IMAGE);
}
String imageUrl = StringUtils.notNullStr(images[0].getSource());
ReaderActivityLauncher.showReaderPhotoViewer(textView.getContext(), imageUrl, null, textView, options, (int) event.getX(), (int) event.getY());
return true;
}
}
try {
return super.onTouchEvent(textView, buffer, event);
} catch (ActivityNotFoundException e) {
AppLog.e(AppLog.T.UTILS, e);
return false;
}
}
use of android.text.style.ImageSpan in project platform_frameworks_base by android.
the class HtmlToSpannedConverter method startImg.
private static void startImg(Editable text, Attributes attributes, Html.ImageGetter img) {
String src = attributes.getValue("", "src");
Drawable d = null;
if (img != null) {
d = img.getDrawable(src);
}
if (d == null) {
d = Resources.getSystem().getDrawable(com.android.internal.R.drawable.unknown_image);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
}
int len = text.length();
text.append("");
text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
use of android.text.style.ImageSpan in project weiciyuan by qii.
the class TimeLineUtility method addEmotions.
private static void addEmotions(SpannableString value) {
Matcher localMatcher = WeiboPatterns.EMOTION_URL.matcher(value);
while (localMatcher.find()) {
String str2 = localMatcher.group(0);
int k = localMatcher.start();
int m = localMatcher.end();
if (m - k < 8) {
Bitmap bitmap = GlobalContext.getInstance().getEmotionsPics().get(str2);
if (bitmap == null) {
bitmap = GlobalContext.getInstance().getHuahuaPics().get(str2);
}
if (bitmap != null) {
ImageSpan localImageSpan = new ImageSpan(GlobalContext.getInstance().getActivity(), bitmap, ImageSpan.ALIGN_BASELINE);
value.setSpan(localImageSpan, k, m, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
use of android.text.style.ImageSpan in project StaticLayoutView by ragnraok.
the class TestSpan method init.
public static void init(Context context) {
for (int i = 0; i <= testString.length(); i++) {
if (i + 1 <= testString.length()) {
ImageSpan imgSpan = new ImageSpan(context, BitmapFactory.decodeResource(context.getResources(), R.drawable.test));
testString.setSpan(imgSpan, i, i + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
}
Random random = new Random();
for (int i = 0; i < allTestStrings.length; i++) {
int start = random.nextInt(testString.length() / 2);
int len = random.nextInt(testString.length());
len = len > 1 ? len : 1;
int end = start + len > testString.length() ? testString.length() : start + len;
allTestStrings[i] = (SpannableString) testString.subSequence(start, end);
}
for (int i = 0; i < testLongString.length(); i++) {
if (i + 1 < testLongString.length()) {
ImageSpan imgSpan = new ImageSpan(context, BitmapFactory.decodeResource(context.getResources(), R.drawable.test));
testLongString.setSpan(imgSpan, i, i + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
}
}
Aggregations