HtmlSpanner 使用小結 — Android解析html

  • 2019 年 11 月 13 日
  • 筆記

如何利用 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;      }  }