android教程之textview解析帶圖片的html樣本_Android

來源:互聯網
上載者:User

複製代碼 代碼如下:

public class MainActivity extends Activity {

 private Handler handler;
 private String html;
 private TextView tv;
 private ProgressBar bar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // 網上找的html資料
  html = "<html><head><title>TextView使用HTML</title></head><body><p><strong>強調</strong></p><p><em>斜體</em></p>"
    + "<p><a href=\"http://www.jb51.net">超連結HTML入門</a>學習HTML!</p><p><font color=\"#aabb00\">顏色1"
    + "</p><p><font color=\"#00bbaa\">顏色2</p><h1>標題1</h1><h3>標題2</h3><h6>標題3</h6><p>大於>小於<</p><p>"
    + "下面是網狀圖片</p><img src=\"http://www.jb51.net/1207.jpg\"/></body>"
    + "下面是網狀圖片</p><img src=\"http://www.jb51.net/207.jpg\"/></body></html>";

  tv = (TextView) this.findViewById(R.id.id);
  bar = (ProgressBar) this.findViewById(R.id.id_bar);
  tv.setMovementMethod(ScrollingMovementMethod.getInstance());// 滾動

  handler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    if (msg.what == 0x101) {
     bar.setVisibility(View.GONE);
     tv.setText((CharSequence) msg.obj);
    }
    super.handleMessage(msg);
   }
  };

  // 因為從網上下載圖片是耗時操作 所以要開啟新線程
  Thread t = new Thread(new Runnable() {
   Message msg = Message.obtain();

   @Override
   public void run() {
    // TODO Auto-generated method stub
    bar.setVisibility(View.VISIBLE);
    /**
     * 要實現圖片的顯示需要使用Html.fromHtml的一個重構方法:public static Spanned
     * fromHtml (String source, Html.ImageGetterimageGetter,
     * Html.TagHandler
     * tagHandler)其中Html.ImageGetter是一個介面,我們要實現此介面,在它的getDrawable
     * (String source)方法中返回圖片的Drawable對象才可以。
     */
    ImageGetter imageGetter = new ImageGetter() {

     @Override
     public Drawable getDrawable(String source) {
      // TODO Auto-generated method stub
      URL url;
      Drawable drawable = null;
      try {
       url = new URL(source);
       drawable = Drawable.createFromStream(
         url.openStream(), null);
       drawable.setBounds(0, 0,
         drawable.getIntrinsicWidth(),
         drawable.getIntrinsicHeight());
      } catch (MalformedURLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      return drawable;
     }
    };
    CharSequence test = Html.fromHtml(html, imageGetter, null);
    msg.what = 0x101;
    msg.obj = test;
    handler.sendMessage(msg);
   }
  });
  t.start();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.