Spannable Vs Typeface Vs Html

Ast*_*hme 10 android textview

我正在准备包含自定义样式的复选框的应用程序,如粗体,斜体,下划线,color.etc ..我有点困惑哪种方法对性能有好处.我尝试过类型面,但是当用户选择两个复选框时,它只显示最后一个值.它不能正常工作.

Man*_*ani 21

字体Paint对象用于绘制文本的图形元素.它指定的字体(例如Monospace,Sans Serif,Serif等)和字体的样式(例如Bold,Italic等),并在内部被两个用于Spannable的Html.

所以性能比较应该在Spannable和之间进行Html.

Html.fromHtml是一个更昂贵的操作,因为它涉及解析Html.我在Traceview中使用了以下代码,并在Html和之间进行了比较Spannable.它基本上将文本设置为粗体并设置超链接.

Debug.startMethodTracing("htmlspan");
Spanned s1 = Html.fromHtml("<b>text1: Constructed from HTML programmatically.</b> Click <a href=\"http://www.google.com\">Link</a> ");
tv1.setText(s1);
Debug.stopMethodTracing();
tv1.setMovementMethod(LinkMovementMethod.getInstance());

Debug.startMethodTracing("normalspan");
SpannableString s2 = new SpannableString("text2: Constructed from JAVA programmatically. Click here.");
s2.setSpan(new StyleSpan(Typeface.BOLD), 0, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s2.setSpan(new URLSpan("http://www.google.com"),53,53+4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv2.setText(s2);
Debug.stopMethodTracing();
tv2.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)

结果:

  • Html API:~14-15ms.(org.ccil.cowan.tagsoup.Parser.parse API大约需要12.282ms)
  • Spannbale API:~2-2ms.

TraceView for Html API: TraceView for Html API

TraceView for Spannable API: TraceView for Spannable API

总结:直接使用性能观点Spannable比较快Html.