Search in sources :

Example 6 with ImageSpan

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
        }
    }
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) AlignmentSpan(android.text.style.AlignmentSpan) Drawable(android.graphics.drawable.Drawable) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) Uri(android.net.Uri) Bitmap(android.graphics.Bitmap) MediaModel(org.wordpress.android.fluxc.model.MediaModel) ImageSpan(android.text.style.ImageSpan) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan)

Example 7 with ImageSpan

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;
    }
}
Also used : PhotoViewerOption(org.wordpress.android.ui.reader.ReaderActivityLauncher.PhotoViewerOption) Layout(android.text.Layout) ActivityNotFoundException(android.content.ActivityNotFoundException) ImageSpan(android.text.style.ImageSpan)

Example 8 with ImageSpan

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);
}
Also used : Drawable(android.graphics.drawable.Drawable) ImageSpan(android.text.style.ImageSpan)

Example 9 with ImageSpan

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);
            }
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Matcher(java.util.regex.Matcher) SpannableString(android.text.SpannableString) ImageSpan(android.text.style.ImageSpan)

Example 10 with ImageSpan

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);
        }
    }
}
Also used : Random(java.util.Random) ImageSpan(android.text.style.ImageSpan)

Aggregations

ImageSpan (android.text.style.ImageSpan)98 SpannableStringBuilder (android.text.SpannableStringBuilder)53 Drawable (android.graphics.drawable.Drawable)29 ForegroundColorSpan (android.text.style.ForegroundColorSpan)23 BackgroundColorSpan (android.text.style.BackgroundColorSpan)14 TextView (android.widget.TextView)14 SpannableString (android.text.SpannableString)12 View (android.view.View)11 UnderlineSpan (android.text.style.UnderlineSpan)10 RelativeSizeSpan (android.text.style.RelativeSizeSpan)7 StyleSpan (android.text.style.StyleSpan)7 Bitmap (android.graphics.Bitmap)6 LinearLayout (android.widget.LinearLayout)6 Application (android.app.Application)5 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)5 CharacterStyle (android.text.style.CharacterStyle)5 SuggestionSpan (android.text.style.SuggestionSpan)5 URLSpan (android.text.style.URLSpan)5 MenuItem (android.view.MenuItem)5 Button (android.widget.Button)5