Android edittext loading HTML content (content including web images)

Source: Internet
Author: User

The html.fromhtml in Android can be used to load Html content. Fromhtml has three parameters to set, the first is to display the HTML content, the second is to say the focus, imagegetter, used to handle the image loading, the third Taghandler can be set to null. Next we will explain the next imagegetter. There are many ways to directly refer to local images. Is synchronous, for example:

Private Imagegetter Imagegetter = new Imagegetter () {@Overridepublic drawable getdrawable (String source) {String F = pic_p Ath.substring (0, 1); String URL = pic_path.substring (2), if (F.equals ("1")) {try {contentresolver CR = Getactivity (). Getcontentresolver (); Uri uri = uri.parse (URL); Bitmap Bitmap = GetImage (CR, URI); return getmydrawable (Bitmap);} catch (Exception e) {e.printstacktrace (); return null;}} else {return null;}};
The above code is a project I have used to refer to the local image method, rewrite Imagegetter, and then use Contentresolver to read the picture converted to bitmap, and then display. But. Very often, we all need to quote a Web image. That's not the way it can be. Very much information was found. Assume that the image is loaded directly inside with an asynchronous method. It shows a square point. So the question is, how should we load the Web images?

first of all. What's the use of creating a urldrawable and letting it inherit bitmapdrawable and rewrite the draw method? This allows you to load the image with the initial image, which is the loaded image.

Urldrawable.java:

public class Urldrawable extends Bitmapdrawable {    //The drawable this need to set, you could set the initial draw ing    //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 (DRA Wable! = null) {            drawable.draw (canvas);}}    }

The next step is to rewrite Imagegetter

Urlimageparser Inheritance Imagegetter

Release source code

Urlimageparser.java

public class Urlimageparser implements Imagegetter {Context C;    EditText container; /*** * Build Urlimageparser will run Asynctask, refresh container * @param t * @param c */public Urlimageparser (EditText T, Con        Text 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 urldrawable I will change from SRC with actual image 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) {//sets the correct binding based on the result of the HTTP call log.d ("height", "" "+result.getintrinsiche             Ight ());             LOG.D ("width", "" +result.getintrinsicwidth ());              Urldrawable.setbounds (0, 0, 0+result.getintrinsicwidth (), 0+result.getintrinsicheight ());             Change the current extractable results from http call urldrawable.drawable = result;            Draw Image Container URLImageParser.this.container.invalidate (); For ICS URLImageParser.this.container.setHeight ((URLImageParser.this.container.getHeight () + Result.getintri            Nsicheight ()));        Pre ICS URLImageParser.this.container.setEllipsize (null); }/*** * Get drawable URL * @param urlstring * @return * */Public drawable FET                Chdrawable (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 {Defa            Ulthttpclient httpClient = new Defaulthttpclient ();            HttpGet request = new HttpGet (urlstring);            HttpResponse response = Httpclient.execute (request);        Return response.getentity (). getcontent (); }    }}

The gaze in the code is also very clear, there is no need to repeatedly explain, the most important thing is to rewrite OnPostExecute. This method is used to refresh the UI after loading, and the ability to redraw the drawable is displayed in the EditText. And does not overlap the position of the text. Isn't that easy?



Android edittext loading HTML content (content including web images)

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.