In Android, Textview displays html text 2-[Textview displays local images]

Source: Internet
Author: User

Textview can display basic HTML tags. If you do not know the tags, you can view the Textview in Android to display html-text-1 ------- [HTML Tag ]!

Next, let's take a look at Textview's display of the "img" label. You may think of building ImageGetter and reload its public Drawable getDrawable (String source) method to get the image of this path.

For example:

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

The following describes the public Drawable getDrawable (String source) method. source is the image path!

For example:

Final String sText = "test image information: <br>  

Source is the src value of img, which is the image path http://pic004.cnblogs.com/news/201211/201211__091749_1.jpg.

Of course, the path can be either a network image, a local image, or a project resource image.

Example: Local Image project resource image

However, different paths have different methods for handling the ImageGetter overload. The following describes various processing methods.

First: Local Images

Final String sText2 = "test image information: 

Type 2: Project Resource Images

Final String sText1 = "test image 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 ;};}

Third: network images

Final String sText = "test image information: <br> 

 

Through these three methods, we can see that different image paths have different processing methods, and you can see clearly what ImageGetter is doing, is to get the image required by src in img!

Note: after obtaining an image, you must set the boundary and boundary of the image, that is, drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); otherwise, Textview cannot display the image.

The above three methods can be used to display images, but I found a problem. The third method is to display network images. I use the Android system to display images, in addition, if the picture is large, the application will be stuck, it must be caused by the use of the main thread to obtain the network image, but if I use a system above android4.0 to run, the image cannot be displayed, only small boxes are displayed.

The cause is that an error is reported during execution on the 4.0 system. The exception is android. OS. the NetworkOnMainThreadException was found in the document. It turns out that the 4.0 system does not allow the main thread (UI thread) to access the network, resulting in exceptions. To put it bluntly, access to the network on the main thread will cause the main thread to be suspended and the system will not allow it to be used.

For details, refer to the next article: Textview with html text in Android 3 ------- [Textview display network image]

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.