【问题标题】:Adding ImageSpan after Html.fromHtml();在 Html.fromHtml() 之后添加 ImageSpan;
【发布时间】:2014-06-05 19:34:44
【问题描述】:

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

<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 加载它们很痛苦。我在 Picasso 和 ImageSpans 上取得了成功。我的问题是,在我使用 HTML.fromHtml(String) 之后可以添加 Imagespans 吗?

如果重要,我想在 EditText 中显示所有内容。

编辑:

如果有人在 EditText 或 TextViews 中显示图像时遇到同样的问题,我可以解决这个烦人的问题:

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);
        }

自定义目标类:

   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) {

    }
}

注意:如果您的图片较大,您应该调整大小

外观:

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

【问题讨论】:

  • 源代码中的目标是什么?

标签: android html spannable


【解决方案1】:

要添加跨度,您需要一个 Spannable 实例。但是,Html.fromHtml() 返回一个 Spanned 对象,它不提供此功能。

这是附加了标记对象的文本界面 它的范围。并非所有文本类都有可变标记或文本;看 Spanable 用于可变标记,Editable 用于可变文本。

但是,您可以很容易地从 Spanned 创建一个 Spannable。只需使用:

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

【讨论】:

  • 经过无数小时的 ImageGetter 痛苦之后,只是为了在 TextView 或 EditText 中显示图像,这就是解决方案。谢谢你,我现在可以快乐地死去。
  • @notsure 不客气。确保这不会很快发生。 ;)
猜你喜欢
  • 2012-08-06
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-06
  • 2019-03-08
  • 2012-08-06
  • 2019-01-06
相关资源
最近更新 更多