Html.fromHtml后添加ImageSpan(); [英] Adding ImageSpan after Html.fromHtml();

查看:588
本文介绍了Html.fromHtml后添加ImageSpan();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,其中包含HTML标记和图像像这样的:

I have an JSON String which contains HTML Tags and image like this one:

<center>denn.. <span class="uicon" style="background-image:url(http://static.allmystery.de/images/lachen.gif);width:15px;height:14px;" title=":)">:)</span>
ich habe immer versucht ein <small>perfektionist</small> zu sein ohne das es mir klar war.<br><br>

我试图取代的笑脸 IMG SRC = 标签,但它是一个痛苦加载它们与ImageGetter.I是成功的毕加索和ImageSpans。我的问题是,它可以添加Imagespans后,我用HTML.fromHtml(字符串)?

I tried to replace the smileys with img src= "" tags but its a pain to load them with an ImageGetter.I was successful with Picasso and ImageSpans. My Question is, it is possible to add Imagespans after I used HTML.fromHtml(String)?

我想在一个EditText显示一切,如果它很重要。

I want to display everything in an EditText if it matters.

编辑:

如果有人在这里显示的EditText上的图像或TextViews的同样的麻烦是我解决这个恼人的问题:

If someone has the same trouble of displaying images in EditText or TextViews here is my solution to this annoying problem:

SpannableString spannableString = new SpannableString(Html.fromHtml(text));

Pattern p = Pattern.compile(ADD REGEX TO FIND IMAGE URLS);
Matcher m = p.matcher(spannableString);

        while (m.find()) {

            //m.start() will give you the startlocation of the matched string
            //m.end() does the same with the endlocation
            //m.group(1) contains the imageurl which is captured with regex

            //CustomTarget implements Target from Picasso (Imageloading library)
            //holder.postText is the EditText or TextView

            CustomTarget target = new CustomTarget(m.group(1), m.start(), m.end(), holder.postText, spannableString);

            //add target to an ArrayList to keep a strong reference to prevent that the target gets garbage collected
            // before the image is placed into the view
            targets.add(target);

            //load the image into the CustomTarget
            Picasso picasso = Picasso.with(context);
            picasso.setDebugging(true);
            picasso.load(m.group(1)).into(target);
        }

CustomTarget类:

CustomTarget Class:

   private class CustomTarget implements Target {

    String url;
    int start;
    int end;
    WeakReference<EditText> postText;
    SpannableString rawText;


    public CustomTarget(String url, int start, int end, EditText postText, SpannableString rawText) {

        this.url = url;
        this.start = start;
        this.end = end;
        this.postText = new WeakReference<>(postText);
        this.rawText = rawText;

    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

        //get a weak Reference to the EditText because holder pattern in ListView will replace the View inside the holder before the Async Task is done
        EditText editText = postText.get();

        //Get existing ImageSpans to add them later again, if not, you will only get the last loaded image displayed
        ImageSpan[] spans =  editText.getText().getSpans(0, editText.length(), ImageSpan.class);

        BitmapDrawable d =  new BitmapDrawable(context.getResources(), bitmap);
        d.setBounds(0, 0, d.getIntrinsicWidth()+5, d.getIntrinsicHeight()+5);
        ImageSpan imageSpan = new ImageSpan( d, ImageSpan.ALIGN_BASELINE);

        //add ImageSpan to the SpannableString
        rawText.setSpan(imageSpan, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        //add previously added ImageSpans
        if (spans.length >= 1) {

            for (ImageSpan image: spans) {


                rawText.setSpan(image, editText.getText().getSpanStart(image), editText.getText().getSpanEnd(image), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

            }

        }
        //add the edited SpannableString to the EditText
        editText.setText(rawText, TextView.BufferType.SPANNABLE);

        //remove target from ArrayList to allow Garbage Collection
        targets.remove(this);

    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
}

请注意:你应该做一些调整大小,如果你的图像是更大

NOTE: you should do some resizing if your images are larger

它的外观:

http://i.imgur.com/Gr3WtyY.png

推荐答案

要添加的跨度,你需要的 Spannable 实例。然而, Html.fromHtml()返回的 跨区 对象,它不提供此。

To add spans, you need a Spannable instance. However, Html.fromHtml() returns a Spanned object, which doesn't provide for this.

这是具有附连到标记的对象的接口文本   它的范围。并非所有的文字类具有可变标记或文字;看到   Spannable的可变标记和可编辑的文本可变

This is the interface for text that has markup objects attached to ranges of it. Not all text classes have mutable markup or text; see Spannable for mutable markup and Editable for mutable text.

不过,您可以创建一个 Spannable 跨区,pretty的轻松。只需使用:

However, you can create a Spannable from a Spanned, pretty easily. Just use:

SpannableString s = new SpannableString(Html.fromHtml("..."));

这篇关于Html.fromHtml后添加ImageSpan();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆