Android Development Tips--solving some problems with TextView loading HTML

Source: Internet
Author: User

A few days ago in a Gradle User Guide application, using the TextView to load HTML content (as for why not WebView, I did not seriously use and compared, perhaps later will change it), which met some tangled problems, fortunately, the main problems are solved.

Here are some of the problems encountered and my solution.

TextView asynchronously loading images and text overlays in HTML

Loading the HTML picture in TextView requires implementing the Html.imagegetter interface, and then fetching the picture in public drawable getdrawable (String source). Image capture I was directly using the Imageloader, one to load pictures easy, and secondly with the cache function, exactly what I want. However, the Getdrawable method is called in the main thread and requires that the return Drawable object be used for display, and we cannot have network access in the main thread, so it needs to be loaded asynchronously. Imageloader itself already has the ability to load asynchronously, so we need to combine it with the Getdrawable method. The method used here and Baidu common to the method is slightly different, because Baidu on the method in my here will have a bug, in the second entry when the picture is often not loaded out.

First, write a class that inherits Bitmapdrawable, with the following code:

Package Com.githang.gradledoc.chapter;import Android.graphics.bitmap;import Android.graphics.canvas;import android.graphics.drawable.bitmapdrawable;/** * User:geek_soledad ([email protected]) * date:2014-11-30 * time:00:09 * FI XME */public class Urldrawable extends Bitmapdrawable {    protected Bitmap Bitmap;    @Override public    void Draw (canvas canvas) {        if (bitmap! = null) {            canvas.drawbitmap (bitmap, 0, 0, getpaint ()) ;        }    }}

This class is used for objects returned in getdrawable.

Then write a class implementation Html.imagegetter interface, the code is as follows:

    public class Urlimageparser implements Html.imagegetter {TextView mtextview;        Public Urlimageparser (TextView TextView) {this.mtextview = TextView; } @Override Public drawable getdrawable (String source) {Final urldrawable urldrawable = new Urld            Rawable ();            LOG.D ("Chapteractivity", Consts.base_url + source);                Imageloader.getinstance (). LoadImage (Consts.base_url + source, new Simpleimageloadinglistener () {@Override public void Onloadingcomplete (String imageuri, view view, Bitmap loadedimage) {Urldrawa                    Ble.bitmap = Loadedimage;                    Urldrawable.setbounds (0, 0, loadedimage.getwidth (), Loadedimage.getheight ());                    Mtextview.invalidate (); Mtextview.settext (Mtextview.gettext ());            Resolve text overlap}});        return urldrawable; }    }
The Mtextview here is the TextView object where we want to load the picture. Here I do not adapt to the image of the adaptive screen, do a relatively simple, just to set the loaded image to the previous return of the Urldrawable object urldrawable to go, and then call the Mtextview.invalidate () method to update the interface.

But after this, there is one more question. Although we set its border on urldrawable called SetBounds, there is still a problem with overlapping images. You need to reset the contents of the Mtextview, as in the code above:

Mtextview.settext (Mtextview.gettext ());

The code to set the HTML to TextView is as follows:

Mdocview.settext (html.fromhtml (Doc, New Urlimageparser (Mdocview), null));

The displayed code format is all messed up TextView Although HTML can be displayed, many tags are not supported. For example, to display the code <pre>,<code> these tags. Originally thought that taghandler can solve this problem, but I read the online introduction to Taghandler, found that this usage does not deal with the problem I met, because it can only deal with the parsed to the label before the content, unable to process the content after the tag. Fortunately, I set the HTML content to TextView before it was useful jsoup to do some processing, and decided to use Jsoup to deal with. The processing method is very simple, the label in the <pre>, replace the newline character with <br/&gt, and replace the space with the same, the code is as follows:
    /**     * Handles code display issues.     *     * @param HTML elements for chapter chapter content.     *    /private void Handlerpretag (Element chapter) {        Elements preelems = Chapter.select ("pre");        for (Element elem:preelems) {            elem.html (elem.html (). ReplaceAll ("\ n", "<br/>"). ReplaceAll ("", ""));        }    }

Last two to solve this problem:

This article original, reproduced please indicate the source: http://blog.csdn.net/maosidiaoxian/article/details/41673425

Android Development Tips--solving some problems with TextView loading HTML

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.