2015-09-04 139 views
1

我需要用HTML实现textview,通常是图像。 我使用的代码,即在这里找到: Android HTML.fromHTML() with images?在textView中的Android HTML,图像大小

URLDrawable.java

public class URLDrawable extends BitmapDrawable { 
    // the drawable that you need to set, you could set the initial drawing 
    // with the loading image if you need to 
    protected Drawable drawable; 

    @Override 
    public void draw(Canvas canvas) { 
     // override the draw to facilitate refresh function later 
     if(drawable != null) { 
      drawable.draw(canvas); 
     } 
    } 
} 

URLImageParser.java

public class URLImageParser implements 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 
      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 { 
       InputStream is = fetch(urlString); 
       Drawable drawable = Drawable.createFromStream(is, "src"); 
       drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 
         + drawable.getIntrinsicHeight()); 
       return drawable; 
      } catch (Exception e) { 
       return null; 
      } 
     } 

     private InputStream fetch(String urlString) throws MalformedURLException, IOException { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(urlString); 
      HttpResponse response = httpClient.execute(request); 
      return response.getEntity().getContent(); 
     } 
    } 
} 

这里我设置我的TextView文本:

URLImageParser parser = new URLImageParser(textView, getActivity()); 
Spanned spanned = Html.fromHtml(myArticle.getDescription(), parser, null); 
textView.setText(spanned); 

,但我发现图片太小:

enter image description here

如果我改变drawable.setBounds(0, 0, 500(just example), 500(just example))

我得到像这样的问题:

enter image description here

我刚才开始我在android的方式,请帮助我。

+1

您是否检查过[WebView](http://developer.android.com/reference/android/webkit/WebView.html)?它看起来像你需要(呈现HTML) – Bonatti

+0

你帮了我很多!这很容易:( – llaerto

+0

很高兴帮助。欢迎使用stackoverflow,祝你的程序好运..如果您需要帮助,请在提问前总是搜索网站......另外,请研究LogCat工具,以当提问时发布问题 – Bonatti

回答

1

Thx给Bonatti的评论。 这是使用WebView的最佳方式

+0

很高兴为您提供帮助,只需告知,当问题得到解答并且不需要进一步帮助时,请点击左侧绿色的V符号标记为已完成。 – Bonatti