Frontier:
Projects usually need to display a piece of text. If you need a specific effect on the text, you need to write a custom span, which will increase the workload. Currently, android supports html text, the following is my summary of the degree and usage of html supported by android.
AndroidSupportedHtmlTwo interfaces
1 Spanned android. text. Html. fromHtml (String source)
The input parameter is (html text)
Currently, android does not support all html tags. Currently, it only supports tags such as text display and paragraphs. For images and other multimedia, some custom tags cannot be recognized.
2 Spanned android. text. Html. fromHtml (String source, ImageGetter imageGetter, TagHandler tagHandler) Source: html text to be processed
ImageGetter: Image Processing (processing image tags in html)
TagHandler: Processing tags (equivalent to processing custom tags, where you can process custom tags)
In the second interface, in actual application, img in html must be processed as a network image, and custom tags such as [Video] must also be processed ], the following describes the implementation of the two parts.
[1] implement ImageGetter to obtain network images
ImageGetter imgGetter = new Html. ImageGetter (){
@ Override
Public Drawable getDrawable (String source ){
Drawable d = null;
Try
{
URL aryURI = new URL (source );
/* Open the connection */
URLConnection conn = aryURI. openConnection ();
Conn. connect ();
/* Convert to InputStream */
InputStream is = conn. getInputStream ();
/* Convert InputStream to Bitmap */
// Bitmap bm = BitmapFactory. decodeStream (is );
/* Disable InputStream */
/* Add an image */
D = Drawable. createFromStream (is, "111 ");
Is. close ();
} Catch (IOException e)
{
E. printStackTrace ();
}
D. setBounds (1, 1, 45, 45 );
Return d;
}
The above code has been verified. Please feel free to copy it.
Note: public Drawable getDrawable (String source)
"Source" indicates the image path in the label.
[2] implement custom TagHandler
Summary of implementation:
Step oneInherit the TagHandler Interface
Step TwoImplementation
Public VoidHandleTag (BooleanOpening, String tag, Editable output,
XMLReader xmlReader)
Interface call process:
The system is parsing html text
Call a tag once upon discovery, as shown in
The parameters passed by the API are handleTag (true, "html", output, xmlReader)
Call the interface when tag closure is found
The parameters passed by the API are handleTag (false, "html", output, xmlReader)
The following example shows how to add a horizontal line to the text in the <strike> label.
public class MyHtmlTagHandler implements TagHandler {
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if(tag.equalsIgnoreCase("strike") || tag.equals("s")) {
processStrike(opening, output);
}
}
private void processStrike(boolean opening, Editable output) {
int len = output.length();
if(opening) {
output.setSpan(new StrikethroughSpan(), len, len, Spannable.SPAN_MARK_MARK);
} else {
Object obj = getLast(output, StrikethroughSpan.class);
int where = output.getSpanStart(obj);
output.removeSpan(obj);
if (where != len) {
output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
private Object getLast(Editable text, Class kind) {
Object[] objs = text.getSpans(0, text.length(), kind);
if (objs.length == 0) {
return null;
} else {
for(int i = objs.length;i>0;i--) {
if(text.getSpanFlags(objs[i-1]) == Spannable.SPAN_MARK_MARK) {
return objs[i-1];
}
}
return null;
}
}
}