HtmlSpanner 使用小结 -- 安卓解析html
Posted 一柒微笑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HtmlSpanner 使用小结 -- 安卓解析html相关的知识,希望对你有一定的参考价值。
如何利用 HtmlSpanner解析 HTML格式 的字符串:
1. GitHub 下载HtmlSpanner项目 https://github.com/NightWhistler/HtmlSpanner
2. 最好是直接放在java目录下在,这样不需要改引用的包路径
3. 引入需要的依赖包
implementation \'net.sourceforge.htmlcleaner:htmlcleaner:2.21\' implementation \'com.osbcp:cssparser:1.7\'
4. 使用方法:
// 页面上用于展示 html格式的文本布局 TextView textView = findViewById(R.id.htmlSpanner); // 直接 new一个 HtmlSpanner对象 HtmlSpanner htmlSpanner = new HtmlSpanner(); // 格式化 // 解析 html得到 spannable对象 Spannable spannable1 = htmlSpanner.fromHtml("<span style=\'color:red\'>html格式的文字1</span>"); // 显示到 TextView上 textView.setText(spannable1);
5. 在使用中遇到的问题——当富文本中颜色格式是rgb格式,解析失败
解决思路:
1. 首先我们解析的是style=\'color:rgb(0,255,255)\' 这种格式,于是看源码觉得 CSSCompiler 这个类很有问题
2. 找与颜色相关的于是就找到了 parseCSSColor( String colorString ) 这个方法,看起来就是转换颜色用的
3. 源码的写法如下:(是没有对于rgb格式的算法,所以不能解析就很合理啦)
4. 想法修改:( 遇到 0rgb格式就先处理成我们的 hex格式,这样不就完美了嘛 )
5. 工具类代码如下:
package com.xxx.xxx.xxx; public class ColorUtil { /** * rgb 格式的颜色转 hex格式颜色 * @param rgb * @return */ public static String rgb2hex(String rgb) { int r = 0; int g = 0; int b = 0; int left = rgb.indexOf("("); int right = rgb.indexOf(")"); if (left > -1 && right > -1 && right > left) { String substring = rgb.substring(left + 1, right); String[] split = substring.split(","); if (split.length == 3){ r = Integer.valueOf(split[0].trim()); g = Integer.valueOf(split[1].trim()); b = Integer.valueOf(split[2].trim()); } } String rFString, rSString, gFString, gSString, bFString, bSString, result; int red, green, blue; int rred, rgreen, rblue; red = r / 16; rred = r % 16; if (red == 10) rFString = "A"; else if (red == 11) rFString = "B"; else if (red == 12) rFString = "C"; else if (red == 13) rFString = "D"; else if (red == 14) rFString = "E"; else if (red == 15) rFString = "F"; else rFString = String.valueOf(red); if (rred == 10) rSString = "A"; else if (rred == 11) rSString = "B"; else if (rred == 12) rSString = "C"; else if (rred == 13) rSString = "D"; else if (rred == 14) rSString = "E"; else if (rred == 15) rSString = "F"; else rSString = String.valueOf(rred); rFString = rFString + rSString; green = g / 16; rgreen = g % 16; if (green == 10) gFString = "A"; else if (green == 11) gFString = "B"; else if (green == 12) gFString = "C"; else if (green == 13) gFString = "D"; else if (green == 14) gFString = "E"; else if (green == 15) gFString = "F"; else gFString = String.valueOf(green); if (rgreen == 10) gSString = "A"; else if (rgreen == 11) gSString = "B"; else if (rgreen == 12) gSString = "C"; else if (rgreen == 13) gSString = "D"; else if (rgreen == 14) gSString = "E"; else if (rgreen == 15) gSString = "F"; else gSString = String.valueOf(rgreen); gFString = gFString + gSString; blue = b / 16; rblue = b % 16; if (blue == 10) bFString = "A"; else if (blue == 11) bFString = "B"; else if (blue == 12) bFString = "C"; else if (blue == 13) bFString = "D"; else if (blue == 14) bFString = "E"; else if (blue == 15) bFString = "F"; else bFString = String.valueOf(blue); if (rblue == 10) bSString = "A"; else if (rblue == 11) bSString = "B"; else if (rblue == 12) bSString = "C"; else if (rblue == 13) bSString = "D"; else if (rblue == 14) bSString = "E"; else if (rblue == 15) bSString = "F"; else bSString = String.valueOf(rblue); bFString = bFString + bSString; result = "#" + rFString + gFString + bFString; return result; } }
以上是关于HtmlSpanner 使用小结 -- 安卓解析html的主要内容,如果未能解决你的问题,请参考以下文章