24

我正在尝试将 HTML 块加载到 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;
        } 
    }
}

}

有任何想法吗?万分感谢。

4

5 回答 5

55

您可以将您的硬币容器 c(视图)更改为 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 的高度

于 2012-04-18T11:35:36.287 回答
2

我没有足够的声望给 Martin S 投票,但他的回答确实很有帮助。如果 TextView 在加载前显示了默认图像,我们可以像这样更改 setHeight() 方法:

    URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
+ result.getIntrinsicHeight()-mDefaultDrawable.getInstrinsicHeight()));
于 2014-05-06T04:42:17.000 回答
0

可能我们不需要将容器从 View 更改为 TextView。

   @Override
    protected void onPostExecute(Drawable result) {
        // set the correct bound according to the result from HTTP call
        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.setMinimumHeight((URLImageParser.this.container.getHeight()+ result.getIntrinsicHeight()));
        URLImageParser.this.container.requestLayout();
        URLImageParser.this.container.invalidate();
    }
于 2013-03-22T03:03:58.417 回答
0

我在这些解决方案中发现了一个有趣的行为:如果图像的加载速度过快并且 textview 尚未呈现(例如,我使用带有缓存的 Okhttp,所以第二次调用非常快),则 textview 大小为 0。

为了解决这个问题,我将 ImageGetter 从 AsyncTask 转换回来,而是启动了一个 AsyncTask,它为我的 TextView 创建 Spanned 并在之后设置文本。

使用此解决方案,无需在每次加载图像时调整 TextView 的大小。

new AsyncTask<TextView, Void, Spanned>() {
      TextView tv;
      @Override
      protected Spanned doInBackground(TextView... params) {
        tv = params[0];
        return Html.fromHtml(feedEntry.getContent(),
                new HttpImageGetter(getActivity(), HttpLoaderImpl.getInstance(getActivity())),
                new Html.TagHandler() {

                  @Override
                  public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
                    //do nothing...
                  }
                });
      }

      @Override
      protected void onPostExecute(final Spanned result) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {

          @Override
          public void run() {
            tv.setText(result);
          }
        });
      }
    }.execute(textView);
于 2015-08-22T20:34:07.180 回答
-2

是否有特殊原因需要将其加载到文本视图中?你能改用WebView吗?

如果您不能使用 Webviews,那么最好的解决方案是不要将图像放在您的文本视图中。将图像放在 ImageView 中。TextViews 没有任何布局引擎功能,您需要弄清楚将图像和文本放在哪里相互关联。它们不是 ViewGroup(如 LinearLayout 或 RelativeLayout),因此没有内部布局指定功能。如果你真的不想使用 webview(以及它拥有的所有漂亮的布局引擎),你将不得不自己弄清楚如何安排单独的 TextViews 和 ImageViews。

于 2011-10-24T01:04:28.773 回答