我试图创建一个包含TextView元素的列表视图,每个元素都包含一个SpannableString。这些TextView的内容是从Html标记中的ArrayList获取的,并使用Html.fromHtml转换为SpannableStrings。现在,Html.fromHtml有很多性能问题。所以我试着制作我自己的Html.fromHtml版本
class NeoHTML extends DefaultHandler {
SpannableStringBuilder s;
String html;
Context context;
public NeoHTML(String html,Context context) {
s = new SpannableStringBuilder("");
this.html = html;
this.context=context;
getXml();
}
public SpannableStringBuilder fromHTML() {
return s;
}
public void getXml() {
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
DefaultHandler defaultHandler = new DefaultHandler() {
int boldTag = 0;
int italicsTag = 0;
int underlineTag = 0;
int pointer = 0;
boolean brpassed=false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("B")) {
boldTag += 1;
}
if (qName.equalsIgnoreCase("I")) {
italicsTag += 1;
}
if (qName.equalsIgnoreCase("U")) {
underlineTag += 1;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
int tstart=start;
if(new String(ch, start, length).startsWith("[ ]")){
s.append("b");
Typeface font = Typeface.createFromAsset(context.getAssets(), "tickfont.ttf");
s.setSpan (new CustomTypefaceSpan("", font),pointer, pointer+1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
pointer+=1;
tstart+=3;
}
else if(new String(ch, start, length).startsWith("[*]")){
s.append("a");
Typeface font = Typeface.createFromAsset(context.getAssets(), "tickfont.ttf");
s.setSpan (new CustomTypefaceSpan("", font),pointer, pointer+1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
pointer+=1;
tstart+=3;
}
if (boldTag > 0 || italicsTag > 0 || underlineTag > 0) {
s.append(new String(ch, tstart, length));
if (boldTag > 0) {
s.setSpan(new StyleSpan(
android.graphics.Typeface.BOLD), pointer,
pointer + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (italicsTag > 0) {
s.setSpan(new StyleSpan(
android.graphics.Typeface.ITALIC), pointer,
pointer + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (underlineTag > 0) {
s.setSpan(new UnderlineSpan(), pointer, pointer
+ length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
s.append(new String(ch, tstart, length));
}
pointer += length;
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase("B")) {
boldTag -= 1;
}
if (qName.equalsIgnoreCase("BR")) {
s.append("\n");
pointer+=1;
brpassed=true;
}
if (qName.equalsIgnoreCase("I")) {
italicsTag -= 1;
}
if (qName.equalsIgnoreCase("U")) {
underlineTag -= 1;
}
}
};
saxParser.parse(new InputSource(new StringReader(html)),
defaultHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是一个简单的基于sax的解析器。我的灵感来自CommonsWare先前的回答Is there a faster way to decode html characters to a string than Html.fromHtml()?。它具有最小的功能(粗体,斜体,下划线和中断)但即便如此,性能也不是那么好。我有一些想法,比如将textview合成到位图并将其缓存在内存中,而不必在listview循环使用时再次重新渲染它。谁能提出任何想法? ..(请避免使用基于NDK的解决方案,因为我从未成功编译任何这些解决方案而且它增加了不必要的复杂性)
答案 0 :(得分:2)
我曾经有一个类似的问题,当我做Html.fromHtml时,文件在某些设备上模糊时必须是粗体。所以这是我的解决方案:
public static Spannable boldFromHtml(Context c, String text, boolean extraBold){
String textTemp = text;
int boldStartPos = 0;
int boldEndPos = 1;
List<int[]> spanList = new LinkedList<int[]>();
while((boldStartPos = textTemp.indexOf("<b>", boldStartPos)) != -1 && (boldEndPos = textTemp.indexOf("</b>", boldStartPos) - 3) != -1){
textTemp = textTemp.replaceFirst("<b>", "").replaceFirst("</b>", "");
spanList.add(new int[]{boldStartPos, boldEndPos});
boldStartPos += 3;
}
Spannable span = new SpannableString(textTemp);
for(int[] selection: spanList){
span.setSpan(new BoldTypefaceSpan("sans-serif", (extraBold) ? MyExtraBoldFont(c) : MyBoldFont(c)), selection[0], selection[1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return span;
}
此功能仅粗体化您的元素,但可以轻松修改它以执行其他标记。