Android ImageGetter 图像重叠文本

标签 android textview html-parsing spanned

我正在尝试使用

将 HTML block 加载到 TextView 中,包括图像
URLImageParser p = new URLImageParser(articleBody, this);
Spanned htmlSpan = Html.fromHtml(parsedString, p, null);
顺便说一下,

parsedString 是 HTML。无论如何,它会加载,但是图像没有为它们创建任何空间供它们坐下,因此它们最终与它们上方的文本重叠。这是我的 URLImageParser 文件:

public class URLImageParser implements Html.ImageGetter {
Context c;
View container;

/***
 * Construct the URLImageParser which will execute AsyncTask and refresh the container
 * @param t
 * @param c
 */
public URLImageParser(View t, Context c) {
    this.c = c;
    this.container = t;
}

public Drawable getDrawable(String source) {
    URLDrawable urlDrawable = new URLDrawable();

    // get the actual source
    ImageGetterAsyncTask asyncTask = 
        new ImageGetterAsyncTask( urlDrawable);

    asyncTask.execute(source);

    // return reference to URLDrawable where I will change with actual image from
    // the src tag
    return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>  {
    URLDrawable urlDrawable;

    public ImageGetterAsyncTask(URLDrawable d) {
        this.urlDrawable = d;
    }

    @Override
    protected Drawable doInBackground(String... params) {
        String source = params[0];
        return fetchDrawable(source);
    }

    @Override
    protected void onPostExecute(Drawable result) {
        // set the correct bound according to the result from HTTP call
        Log.d("height",""+result.getIntrinsicHeight());
        Log.d("width",""+result.getIntrinsicWidth());
        urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight()); 

        // change the reference of the current drawable to the result
        // from the HTTP call
        urlDrawable.drawable = result;

        // redraw the image by invalidating the container
        URLImageParser.this.container.invalidate();
    }

    /***
     * Get the Drawable from URL
     * @param urlString
     * @return
     */
    public Drawable fetchDrawable(String urlString) {
        try {
            URL aURL = new URL(urlString);
            final URLConnection conn = aURL.openConnection(); 
            conn.connect(); 
            final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
            final Bitmap bm = BitmapFactory.decodeStream(bis);
            Drawable drawable = new BitmapDrawable(bm);
            drawable.setBounds(0,0,bm.getWidth(),bm.getHeight());
            return drawable;
        } catch (Exception e) {
            return null;
        } 
    }
}

}

有什么想法吗?非常感谢。

最佳答案

您可以将您的硬币容器 c( View )更改为 textView,然后让您的 onPostExecute 看起来像这样:

@Override 
protected void onPostExecute(Drawable result) { 
    // set the correct bound according to the result from HTTP call 
    Log.d("height",""+result.getIntrinsicHeight()); 
    Log.d("width",""+result.getIntrinsicWidth()); 
    urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight());  

    // change the reference of the current drawable to the result 
    // from the HTTP call 
    urlDrawable.drawable = result; 

    // redraw the image by invalidating the container 
    URLImageParser.this.container.invalidate();

    // For ICS
    URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
    + result.getIntrinsicHeight()));

    // Pre ICS
    URLImageParser.this.textView.setEllipsize(null);
} 

这将首先绘制图像,然后立即将 TextView 的高度设置为 drawable 的高度 + TextViews 的高度

关于Android ImageGetter 图像重叠文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7870312/

相关文章:

android - 如何在不替换旧文本的情况下向 TextView 添加文本?

android - 如何设置 Picasso 占位符 scaleType?

android - 从 IntentService 启动 Activity 在 Android 12 中不起作用

java - setTextSize 方法后 textView.getLineCount() 返回 0

Android - 在 onMeasure 中更改布局子项

java - 如何从 Jsoup 获取未格式化的 html

android - 在 Android 中从互联网获取数据

php - 您如何在PHP中解析和处理HTML/XML?

java - Android Java 复习没有点击

java - 如何从房间数据库读取所有值并显示到recyclerview中?