Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在Html.fromHtml()之后添加ImageSpan;_Android_Html_Spannable - Fatal编程技术网

Android 在Html.fromHtml()之后添加ImageSpan;

Android 在Html.fromHtml()之后添加ImageSpan;,android,html,spannable,Android,Html,Spannable,我有一个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>per

我有一个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>
自定义目标类:

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

    }
}
私有类CustomTarget实现目标{
字符串url;
int启动;
内端;
WeakReference postText;
SpannableString原始文本;
公共CustomTarget(字符串url、int-start、int-end、EditText-postText、SpannableString-rawText){
this.url=url;
this.start=start;
this.end=end;
this.postText=新的WeakReference(postText);
this.rawText=rawText;
}
@凌驾
已加载位图(位图位图,Picasso.LoadedFrom){
//获取对EditText的弱引用,因为ListView中的holder模式将在异步任务完成之前替换holder中的视图
EditText=postText.get();
//获取现有的ImageSpans以在以后再次添加它们,如果没有,则只会显示上次加载的图像
ImageSpan[]span=editText.getText().Getspan(0,editText.length(),ImageSpan.class);
BitmapDrawable d=新的BitmapDrawable(context.getResources(),位图);
d、 setBounds(0,0,d.getIntrinsicWidth()+5,d.getIntrinsicHeight()+5);
ImageSpan ImageSpan=新的ImageSpan(d,ImageSpan.ALIGN_基线);
//将ImageSpan添加到SpannableString
rawText.setSpan(图像SPAN、开始、结束、可扩展.SPAN_包括在内);
//添加以前添加的ImageSpans
如果(spans.length>=1){
用于(图像跨度图像:跨度){
rawText.setSpan(image,editText.getText().getSpanStart(image),editText.getText().getSpanEnd(image),SPAN.SPAN(包含)和SPAN);
}
}
//将编辑的SpannableString添加到编辑文本中
editText.setText(rawText、TextView.BufferType.Spanable);
//从ArrayList中删除目标以允许垃圾收集
目标。移除(此);
}
@凌驾
公共无效onBitmapFailed(Drawable errorDrawable){
}
@凌驾
准备加载时的公共无效(可提取占位符可提取){
}
}
注意:如果图像较大,则应进行一些调整大小

外观:

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

这是附加了标记对象的文本的接口 它的范围。并非所有文本类都具有可变标记或文本;看见 span可用于可变标记,可编辑用于可变文本

但是,您可以很容易地从跨越的
创建
Spannable
。只需使用:

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

在ImageGetter经历了无数个小时的痛苦之后,仅仅为了在TextView或EditText中显示图像,这就是解决方案。谢谢你,我现在可以快乐地死去了。@不客气。确保这不会发生得太快源代码中的目标是什么?
SpannableString s = new SpannableString(Html.fromHtml("..."));