Andriod TextView Load HTML tags and load labels with pictures

Source: Internet
Author: User
Tags html tags

One, loading HTML code

String html= "<a href=\" http://www.111cn.net "> Baidu </a>;";
Textview.setmovementmethod (Linkmovementmethod.getinstance ());//Allow hyperlinks to click Jump to Browser
Textview.settext (html.fromhtml (Html));


Second, load SRC label

1. Load local picture

String html= "Textview.settext (html.fromhtml (Html, New Html.imagegetter () {
@Override
Public drawable getdrawable (String source) {
Drawable drawable = null;
try {
drawable = Drawable.createfromstream (new URL (source). OpenStream (), NULL);
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());
catch (IOException e) {
E.printstacktrace ();
}
return drawable;
}
},null));

Note: file:/don't forget to add

HTML also has a three-parameter fronhtml method, with three parameters: String Source (the path to the picture resource), Imagegetter Imagegetter (the object that implements the Imagegetter class), Taghandler Taghandler (I don't know what to do, but it's OK to set it to null)

Imagegetter class is a static interface of the HTML class, there is a method getdrawable, the return value is drawable, the function is to obtain the picture drawable

drawable = Drawable.createfromstream (new URL (source). OpenStream (), NULL);

This code, one thing to say, do not use a lot of people on the Internet to use:

drawable= Drawable.createfrompath (source);

A filenotfound error may be reported.


2. Load Network picture

The HTML code is similar to the previous, the path to the network image of the address on the line

Like what:

String html= "But Imagegetter's Getdrawable method needs to change:

Here use the Piccsso frame load network picture, not the classmate can go to Baidu

Private class Myimagegetter implements Html.imagegetter {
Private urldrawable urldrawable = null;

@Override
Public drawable getdrawable (final String source) {
urldrawable = new Urldrawable ();
Picasso.with. Load (source). into (new Target () {
@Override
public void onbitmaploaded (Bitmap Bitmap, picasso.loadedfrom from) {
Urldrawable.bitmap = bitmap;
Urldrawable.setbounds (0, 0, bitmap.getwidth (), Bitmap.getheight ());
Textview.settext (Textview.gettext ())//Without this sentence does not show the picture, for unknown reasons
}

@Override
public void onbitmapfailed (drawable errordrawable) {
}

@Override
public void Onprepareload (drawable placeholderdrawable) {
}
});
return urldrawable;
}
}

Urldrawable class:

/**
* Created by the Administrator on 2016/6/28 0028.
*/
public class Urldrawable extends Bitmapdrawable {
Public Bitmap Bitmap;

@Override
public void Draw (Canvas Canvas) {
Super.draw (canvas);
if (bitmap!= null) {
Canvas.drawbitmap (bitmap, 0, 0, Getpaint ());
}
}
}

The method is invoked as follows:

Textview.settext (html.fromhtml (Html, New Myimagegetter (), null));

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.