Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Textview to display html text in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "how to use Textview to display html text in Android", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use Textview to display html text in Android" can help you solve your doubts.

The getDrawable (String source) method to get the picture of the path.

For example:

Final Html.ImageGetter imageGetter = new Html.ImageGetter () {public Drawable getDrawable (String source) {return drawable;};}

Next to the public Drawable getDrawable (String source) this method, source is the picture path!

For example:

Final String sText = "Test picture information:

TView.setText (Html.fromHtml (sText, imageGetter, null))

Then source is the value of the src of img, which is the image path of http://pic004.cnblogs.com/news/201211/20121108_091749_1.jpg.

Of course, this.

This path can be either a network picture, a local picture or a project resource picture.

For example: local picture

Project resource picture

But different paths, ImageGetter overload processing methods are different, the following one by one to introduce a variety of processing methods.

* species: local pictures

Final String sText2 = "Test picture information:

TView.setText (Html.fromHtml (sText2, imageGetter, null)); final Html.ImageGetter imageGetter = new Html.ImageGetter () {public Drawable getDrawable (String source) {Drawable drawable=null; drawable=Drawable.createFromPath (source); drawable.setBounds (0,0, drawable.getIntrinsicWidth (), drawable.getIntrinsicHeight ()); return drawable;};}

The second kind: project resource picture

Final String sText1 = "Test picture information:

TView.setText (Html.fromHtml (sText1, imageGetter, null)); final Html.ImageGetter imageGetter = new Html.ImageGetter () {public Drawable getDrawable (String source) {Drawable drawable=null; int rId=Integer.parseInt (source); drawable=getResources (). GetDrawable (rId); drawable.setBounds (0,0, drawable.getIntrinsicWidth (), drawable.getIntrinsicHeight ()); return drawable;};}

The third kind: network pictures

Final String sText = "Test picture information:

TView.setText (Html.fromHtml (sText, imageGetter, null)); final Html.ImageGetter imageGetter = new Html.ImageGetter () {public Drawable getDrawable (String source) {Drawable drawable=null; URL url; try {url = new URL (source); drawable= Drawable.createFromStream (url.openStream (), ");} catch (Exception e) {e.printStackTrace (); return null } drawable.setBounds (0,0, drawable.getIntrinsicWidth (), drawable.getIntrinsicHeight ()); return drawable;};}

Through these three ways, we can see that different picture paths lead to different ways of dealing with pictures, and we can also see at a glance what ImageGetter does, that is, to get the pictures needed by src in img!

One reminder: after getting a picture, be sure to set the boundary of the picture, namely: drawable.setBounds (0,0, drawable.getIntrinsicWidth (), drawable.getIntrinsicHeight ()); otherwise, after getting the picture, Textview cannot display the picture.

Through the above three ways, it is possible to display pictures, but I found a problem, that is, the third, display network pictures, I use android2.3 system, can display pictures out, and if the picture is relatively large, the application will be stuck, it must be caused by using the main thread to get network pictures, but if I run with a system above android4.0, I cannot display pictures. Only small boxes are displayed.

Read here, this article "how to use Textview to display html text in Android" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report