一、TvUtils 工具类,主要帮助实现富文本相关功能

  1. public class TvUtils {
  2. /**
  3. * 富文本设置字体大小
  4. *
  5. * @param srcString
  6. * @param tartgetString
  7. * @param txtSize
  8. * @return
  9. */
  10. public static SpannableString setTextPartTextSize(String srcString, String tartgetString, int txtSize) {
  11. int position = srcString.indexOf(tartgetString);
  12. SpannableString mSp = new SpannableString(srcString);
  13. if (position >= 0) {
  14. mSp.setSpan(new AbsoluteSizeSpan(txtSize, false), position, position + tartgetString.length(),
  15. Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  16. }
  17. return mSp;
  18. }
  19. /**富文本设置字体颜色和大小
  20. * @param srcString
  21. * @param tartgetString
  22. * @param targetColor
  23. * @param txtSizeDp
  24. * @return
  25. */
  26. public static SpannableString setTextPartTextSize(String srcString, String tartgetString, int targetColor, int txtSizeDp) {
  27. int position = srcString.indexOf(tartgetString);
  28. SpannableString mSp = new SpannableString(srcString);
  29. if (position >= 0) {
  30. mSp.setSpan(new AbsoluteSizeSpan(txtSizeDp, true), position, position + tartgetString.length(),
  31. Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  32. mSp.setSpan(new ForegroundColorSpan(targetColor), position, position + tartgetString.length(),
  33. Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  34. }
  35. return mSp;
  36. }
  37. /**富文本设置部分文字颜色
  38. * @param srcString
  39. * @param tartgetString
  40. * @param targetColor
  41. * @return
  42. */
  43. public static SpannableString setTextPartColor(String srcString,String tartgetString,int targetColor){
  44. int position = srcString.indexOf(tartgetString);
  45. SpannableString mSp = new SpannableString(srcString);
  46. if (position >= 0) {
  47. mSp.setSpan(new ForegroundColorSpan(targetColor), position, position + tartgetString.length(),
  48. Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  49. }
  50. return mSp;
  51. }
  52. /**富文本设置部分文字背景色
  53. * @param srcString
  54. * @param tartgetString
  55. * @param targetColor
  56. * @return
  57. */
  58. public static SpannableString setTextPartBg(String srcString,String tartgetString,int targetColor){
  59. int position = srcString.indexOf(tartgetString);
  60. SpannableString mSp = new SpannableString(srcString);
  61. if (position >= 0) {
  62. mSp.setSpan(new BackgroundColorSpan(targetColor), position, position + tartgetString.length(),
  63. Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  64. }
  65. return mSp;
  66. }
  67. /**设置中划线
  68. * @param src
  69. * @param start
  70. * @param end
  71. * @return
  72. */
  73. public static SpannableString setStrikethrough(String src,int start,int end){
  74. SpannableString spannableString = new SpannableString(src);
  75. StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
  76. spannableString.setSpan(strikethroughSpan, start,end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  77. return spannableString;
  78. }
  79. /**设置下划线
  80. * @param src
  81. * @param start
  82. * @param end
  83. * @return
  84. */
  85. public static SpannableString setUnderline(String src,int start,int end){
  86. SpannableString spannableString = new SpannableString(src);
  87. UnderlineSpan underlineSpan = new UnderlineSpan();
  88. spannableString.setSpan(underlineSpan, start,end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  89. return spannableString;
  90. }
  91. /**同时设置下划线和中划线
  92. * @param src
  93. * @param up
  94. * @param down
  95. * @return
  96. */
  97. public static SpannableString setUpDownLine(String src,String up,String down){
  98. int uPosition = src.indexOf(up);
  99. int dPosition = src.indexOf(down);
  100. SpannableString mSp = new SpannableString(src);
  101. UnderlineSpan underlineSpan = new UnderlineSpan();
  102. StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
  103. if (uPosition >= 0) {
  104. mSp.setSpan(strikethroughSpan, uPosition,uPosition+up.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  105. }
  106. if (dPosition >= 0) {
  107. mSp.setSpan(underlineSpan, dPosition,dPosition+down.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  108. }
  109. return mSp;
  110. }
  111. /**设置上标
  112. * @param src
  113. * @param start
  114. * @param end
  115. * @return
  116. */
  117. public static SpannableString setUpScript(String src,int start,int end){
  118. SpannableString spannableString = new SpannableString(src);
  119. SuperscriptSpan superscriptSpan = new SuperscriptSpan();
  120. spannableString.setSpan(superscriptSpan, start,end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  121. return spannableString;
  122. }
  123. /**设置下标
  124. * @param src
  125. * @param start
  126. * @param end
  127. * @return
  128. */
  129. public static SpannableString setDownScript(String src,int start,int end){
  130. SpannableString spannableString = new SpannableString(src);
  131. SubscriptSpan subscriptSpan = new SubscriptSpan();
  132. spannableString.setSpan(subscriptSpan, start,end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  133. return spannableString;
  134. }
  135. /**设置部分文字加粗
  136. * @param src
  137. * @param start
  138. * @param end
  139. * @return
  140. */
  141. public static SpannableString setTextBold(String src,int start,int end){
  142. SpannableString spannableString = new SpannableString(src);
  143. StyleSpan styleSpan_B = new StyleSpan(Typeface.BOLD);
  144. //StyleSpan styleSpan_I = new StyleSpan(Typeface.ITALIC);
  145. spannableString.setSpan(styleSpan_B, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  146. // spannableString.setSpan(styleSpan_I, 8, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  147. // textView.setHighlightColor(Color.parseColor("#36969696"));
  148. // textView.setText(spannableString);
  149. return spannableString;
  150. }
  151. /**设置部分文字倾斜
  152. * @param src
  153. * @param start
  154. * @param end
  155. * @return
  156. */
  157. public static SpannableString setTextItalic(String src,int start,int end){
  158. SpannableString spannableString = new SpannableString(src);
  159. //StyleSpan styleSpan_B = new StyleSpan(Typeface.BOLD);
  160. StyleSpan styleSpan_I = new StyleSpan(Typeface.ITALIC);
  161. //spannableString.setSpan(styleSpan_B, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  162. spannableString.setSpan(styleSpan_I, 8, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  163. // textView.setHighlightColor(Color.parseColor("#36969696"));
  164. // textView.setText(spannableString);
  165. return spannableString;
  166. }
  167. public static SpannableString setTextIcon(String src, int start, int end, Drawable drawable){
  168. SpannableString spannableString = new SpannableString("在文本中添加表情(表情)");
  169. //Drawable drawable = getResources().getDrawable(R.mipmap.icon_one);
  170. drawable.setBounds(0, 0, 42, 42);
  171. ImageSpan imageSpan = new ImageSpan(drawable);
  172. spannableString.setSpan(imageSpan, 6, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  173. return spannableString;
  174. }
  175. }

二、SpannableStringUtils,功能有重复

    1. 使用效果图

image.png

    1. 使用示例 ```java val create1 = SpannableStringUtils.getBuilder(“普通样式的文字,加粗”)

      1. .setBold()
      2. .create()

      tv_textview_blod.text = create1

      val create2 = SpannableStringUtils.getBuilder(“普通样式的文字,加粗倾斜”)

      1. .setBoldItalic()
      2. .create()

      tv_textview_blodItalic.text = create2

      val create3 = SpannableStringUtils.getBuilder(“普通样式的文字,下划线”)

      1. .setUnderline()
      2. .create()

      tv_textview_three.text = create3

      val create4 = SpannableStringUtils.getBuilder(“普通样式的文字,设置标识”)

      1. .setFlag(SPAN_INCLUSIVE_EXCLUSIVE)
      2. .create()

      tv_textview_4.text = create4

      val create5 = SpannableStringUtils.getBuilder(“普通样式的文字,设置foregroundcolor”)

      1. .setForegroundColor(ContextCompat.getColor(this,R.color.red))
      2. .create()

      tv_textview_5.text = create5

      val create6 = SpannableStringUtils.getBuilder(“普通样式的文字,设置backgroundcolor”)

      1. .setBackgroundColor(ContextCompat.getColor(this,R.color.red))
      2. .create()

      tv_textview_6.text = create6

      val create7 = SpannableStringUtils.getBuilder(“普通样式的文字,设置引用线颜色”)

      1. .setQuoteColor(ContextCompat.getColor(this,R.color.red))
      2. .create()

      tv_textview_7.text = create7

      val create8 = SpannableStringUtils.getBuilder(“普通样式的文字,设置列表标记”)

      1. .setBullet(20,ContextCompat.getColor(this,R.color.red))
      2. .create()

      tv_textview_8.text = create8

      val create9 = SpannableStringUtils.getBuilder(“普通样式的文字,设置字体比例”)

      1. .setProportion(2f)
      2. .create()

      tv_textview_9.text = create9

      val create10 = SpannableStringUtils.getBuilder(“普通样式的文字,设置字体横向比例”)

      1. .setXProportion(2f)
      2. .create()

      tv_textview_10.text = create10

      val create11 = SpannableStringUtils.getBuilder(“普通样式的文字,设置删除线”)

      1. .setStrikethrough()
      2. .create()

      tv_textview_11.text = create11

      val create12 = SpannableStringUtils.getBuilder(“普通样式的文字,设置上标”)

      1. .setSuperscript()
      2. .create()

      tv_textview_12.text = create12

      val create13 = SpannableStringUtils.getBuilder(“普通样式的文字,设置下标”)

      1. .setSuperscript()
      2. .create()

      tv_textview_13.text = create13

      //设置了图片以后就没有文字了 val create14 = SpannableStringUtils.getBuilder(“普通样式的文字,设置图片”)

      1. .setResourceId(R.mipmap.ic_launcher)
      2. .create()

      //tv_textview_14.text = create14

      //没什么效果,可能用法不对 tv_textview_15.movementMethod = LinkMovementMethod.getInstance() val create15 = SpannableStringUtils.getBuilder(“普通样式的文字,设置点击”)

      1. .setClickSpan(object :ClickableSpan(){
      2. override fun onClick(widget: View) {
      3. Toast.makeText(this@TextViewActivity, "hello", Toast.LENGTH_SHORT).show()
      4. }
      5. })
      6. .create()

      tv_textview_15.text = create15

      val create16 = SpannableStringUtils.getBuilder(“普通样式的文字,设置超链接”)

      1. .setForegroundColor(ContextCompat.getColor(this,R.color.red))
      2. .setUrl("https://www.baidu.com")
      3. .create()

      tv_textview_16.movementMethod = LinkMovementMethod.getInstance() tv_textview_16.text = create16 tv_textview_16.setTextColor(ContextCompat.getColor(this,R.color.red)) // tv_textview_16.setOnClickListener { // Toast.makeText(this@TextViewActivity, “hello”, Toast.LENGTH_SHORT).show() // }

  1. - 3. 工具类
  2. ```java
  3. public class SpannableStringUtils {
  4. private SpannableStringUtils() {
  5. throw new UnsupportedOperationException("u can't instantiate me...");
  6. }
  7. /**
  8. * 获取建造者
  9. *
  10. * @param text 样式字符串文本
  11. * @return {@link Builder}
  12. */
  13. public static Builder getBuilder(@NonNull CharSequence text) {
  14. return new Builder(text);
  15. }
  16. public static class Builder {
  17. private int defaultValue = 0x12000000;
  18. private CharSequence text;
  19. private int flag;
  20. @ColorInt
  21. private int foregroundColor;
  22. @ColorInt
  23. private int backgroundColor;
  24. @ColorInt
  25. private int quoteColor;
  26. private boolean isLeadingMargin;
  27. private int first;
  28. private int rest;
  29. private boolean isBullet;
  30. private int gapWidth;
  31. private int bulletColor;
  32. private float proportion;
  33. private float xProportion;
  34. private boolean isStrikethrough;
  35. private boolean isUnderline;
  36. private boolean isSuperscript;
  37. private boolean isSubscript;
  38. private boolean isBold;
  39. private boolean isItalic;
  40. private boolean isBoldItalic;
  41. private String fontFamily;
  42. private Layout.Alignment align;
  43. private boolean imageIsBitmap;
  44. private Bitmap bitmap;
  45. private boolean imageIsDrawable;
  46. private Drawable drawable;
  47. private boolean imageIsUri;
  48. private Uri uri;
  49. private boolean imageIsResourceId;
  50. @DrawableRes
  51. private int resourceId;
  52. private ClickableSpan clickSpan;
  53. private String url;
  54. private boolean isBlur;
  55. private float radius;
  56. private BlurMaskFilter.Blur style;
  57. private SpannableStringBuilder mBuilder;
  58. private Builder(@NonNull CharSequence text) {
  59. this.text = text;
  60. flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
  61. foregroundColor = defaultValue;
  62. backgroundColor = defaultValue;
  63. quoteColor = defaultValue;
  64. proportion = -1;
  65. xProportion = -1;
  66. mBuilder = new SpannableStringBuilder();
  67. }
  68. /**
  69. * 设置标识
  70. *
  71. * @param flag <ul>
  72. * <li>{@link Spanned#SPAN_INCLUSIVE_EXCLUSIVE}</li>
  73. * <li>{@link Spanned#SPAN_INCLUSIVE_INCLUSIVE}</li>
  74. * <li>{@link Spanned#SPAN_EXCLUSIVE_EXCLUSIVE}</li>
  75. * <li>{@link Spanned#SPAN_EXCLUSIVE_INCLUSIVE}</li>
  76. * </ul>
  77. * @return {@link Builder}
  78. */
  79. public Builder setFlag(int flag) {
  80. this.flag = flag;
  81. return this;
  82. }
  83. /**
  84. * 设置前景色
  85. *
  86. * @param color 前景色
  87. * @return {@link Builder}
  88. */
  89. public Builder setForegroundColor(@ColorInt int color) {
  90. this.foregroundColor = color;
  91. return this;
  92. }
  93. /**
  94. * 设置背景色
  95. *
  96. * @param color 背景色
  97. * @return {@link Builder}
  98. */
  99. public Builder setBackgroundColor(@ColorInt int color) {
  100. this.backgroundColor = color;
  101. return this;
  102. }
  103. /**
  104. * 设置引用线的颜色
  105. *
  106. * @param color 引用线的颜色
  107. * @return {@link Builder}
  108. */
  109. public Builder setQuoteColor(@ColorInt int color) {
  110. this.quoteColor = color;
  111. return this;
  112. }
  113. /**
  114. * 设置缩进
  115. *
  116. * @param first 首行缩进
  117. * @param rest 剩余行缩进
  118. * @return {@link Builder}
  119. */
  120. public Builder setLeadingMargin(int first, int rest) {
  121. this.first = first;
  122. this.rest = rest;
  123. isLeadingMargin = true;
  124. return this;
  125. }
  126. /**
  127. * 设置列表标记
  128. *
  129. * @param gapWidth 列表标记和文字间距离
  130. * @param color 列表标记的颜色
  131. * @return {@link Builder}
  132. */
  133. public Builder setBullet(int gapWidth, int color) {
  134. this.gapWidth = gapWidth;
  135. bulletColor = color;
  136. isBullet = true;
  137. return this;
  138. }
  139. /**
  140. * 设置字体比例
  141. *
  142. * @param proportion 比例
  143. * @return {@link Builder}
  144. */
  145. public Builder setProportion(float proportion) {
  146. this.proportion = proportion;
  147. return this;
  148. }
  149. /**
  150. * 设置字体横向比例
  151. *
  152. * @param proportion 比例
  153. * @return {@link Builder}
  154. */
  155. public Builder setXProportion(float proportion) {
  156. this.xProportion = proportion;
  157. return this;
  158. }
  159. /**
  160. * 设置删除线
  161. *
  162. * @return {@link Builder}
  163. */
  164. public Builder setStrikethrough() {
  165. this.isStrikethrough = true;
  166. return this;
  167. }
  168. /**
  169. * 设置下划线
  170. *
  171. * @return {@link Builder}
  172. */
  173. public Builder setUnderline() {
  174. this.isUnderline = true;
  175. return this;
  176. }
  177. /**
  178. * 设置上标
  179. *
  180. * @return {@link Builder}
  181. */
  182. public Builder setSuperscript() {
  183. this.isSuperscript = true;
  184. return this;
  185. }
  186. /**
  187. * 设置下标
  188. *
  189. * @return {@link Builder}
  190. */
  191. public Builder setSubscript() {
  192. this.isSubscript = true;
  193. return this;
  194. }
  195. /**
  196. * 设置粗体
  197. *
  198. * @return {@link Builder}
  199. */
  200. public Builder setBold() {
  201. isBold = true;
  202. return this;
  203. }
  204. /**
  205. * 设置斜体
  206. *
  207. * @return {@link Builder}
  208. */
  209. public Builder setItalic() {
  210. isItalic = true;
  211. return this;
  212. }
  213. /**
  214. * 设置粗斜体
  215. *
  216. * @return {@link Builder}
  217. */
  218. public Builder setBoldItalic() {
  219. isBoldItalic = true;
  220. return this;
  221. }
  222. /**
  223. * 设置字体
  224. *
  225. * @param fontFamily 字体
  226. * <ul>
  227. * <li>monospace</li>
  228. * <li>serif</li>
  229. * <li>sans-serif</li>
  230. * </ul>
  231. * @return {@link Builder}
  232. */
  233. public Builder setFontFamily(@Nullable String fontFamily) {
  234. this.fontFamily = fontFamily;
  235. return this;
  236. }
  237. /**
  238. * 设置对齐
  239. *
  240. * @param align 对其方式
  241. * <ul>
  242. * Alignment#ALIGN_NORMAL}正常</li>
  243. * Alignment#ALIGN_OPPOSITE}相反</li>
  244. * Alignment#ALIGN_CENTER}居中</li>
  245. * </ul>
  246. * @return {@link Builder}
  247. */
  248. public Builder setAlign(@Nullable Layout.Alignment align) {
  249. this.align = align;
  250. return this;
  251. }
  252. /**
  253. * 设置图片
  254. *
  255. * @param bitmap 图片位图
  256. * @return {@link Builder}
  257. */
  258. public Builder setBitmap(@NonNull Bitmap bitmap) {
  259. this.bitmap = bitmap;
  260. imageIsBitmap = true;
  261. return this;
  262. }
  263. /**
  264. * 设置图片
  265. *
  266. * @param drawable 图片资源
  267. * @return {@link Builder}
  268. */
  269. public Builder setDrawable(@NonNull Drawable drawable) {
  270. this.drawable = drawable;
  271. imageIsDrawable = true;
  272. return this;
  273. }
  274. /**
  275. * 设置图片
  276. *
  277. * @param uri 图片uri
  278. * @return {@link Builder}
  279. */
  280. public Builder setUri(@NonNull Uri uri) {
  281. this.uri = uri;
  282. imageIsUri = true;
  283. return this;
  284. }
  285. /**
  286. * 设置图片
  287. *
  288. * @param resourceId 图片资源id
  289. * @return {@link Builder}
  290. */
  291. public Builder setResourceId(@DrawableRes int resourceId) {
  292. this.resourceId = resourceId;
  293. imageIsResourceId = true;
  294. return this;
  295. }
  296. /**
  297. * 设置点击事件
  298. * <p>需添加view.setMovementMethod(LinkMovementMethod.getInstance())</p>
  299. *
  300. * @param clickSpan 点击事件
  301. * @return {@link Builder}
  302. */
  303. public Builder setClickSpan(@NonNull ClickableSpan clickSpan) {
  304. this.clickSpan = clickSpan;
  305. return this;
  306. }
  307. /**
  308. * 设置超链接
  309. * <p>需添加view.setMovementMethod(LinkMovementMethod.getInstance())</p>
  310. *
  311. * @param url 超链接
  312. * @return {@link Builder}
  313. */
  314. public Builder setUrl(@NonNull String url) {
  315. this.url = url;
  316. return this;
  317. }
  318. /**
  319. * 设置模糊
  320. * <p>尚存bug,其他地方存在相同的字体的话,相同字体出现在之前的话那么就不会模糊,出现在之后的话那会一起模糊</p>
  321. * <p>推荐还是把所有字体都模糊这样使用</p>
  322. *
  323. * @param radius 模糊半径(需大于0)
  324. * @param style 模糊样式<ul>
  325. * Blur # NORMAL}</li>
  326. * Blur #SOLID}</li>
  327. * Blur #OUTER}</li>
  328. * Blur #INNER}</li>
  329. * </ul>
  330. * @return {@link Builder}
  331. */
  332. public Builder setBlur(float radius, BlurMaskFilter.Blur style) {
  333. this.radius = radius;
  334. this.style = style;
  335. this.isBlur = true;
  336. return this;
  337. }
  338. /**
  339. * 追加样式字符串
  340. *
  341. * @param text 样式字符串文本
  342. * @return {@link Builder}
  343. */
  344. public Builder append(@NonNull CharSequence text) {
  345. setSpan();
  346. this.text = text;
  347. return this;
  348. }
  349. /**
  350. * 创建样式字符串
  351. *
  352. * @return 样式字符串
  353. */
  354. public SpannableStringBuilder create() {
  355. setSpan();
  356. return mBuilder;
  357. }
  358. /**
  359. * 设置样式
  360. */
  361. private void setSpan() {
  362. int start = mBuilder.length();
  363. mBuilder.append(this.text);
  364. int end = mBuilder.length();
  365. if (foregroundColor != defaultValue) {
  366. mBuilder.setSpan(new ForegroundColorSpan(foregroundColor), start, end, flag);
  367. foregroundColor = defaultValue;
  368. }
  369. if (backgroundColor != defaultValue) {
  370. mBuilder.setSpan(new BackgroundColorSpan(backgroundColor), start, end, flag);
  371. backgroundColor = defaultValue;
  372. }
  373. if (isLeadingMargin) {
  374. mBuilder.setSpan(new LeadingMarginSpan.Standard(first, rest), start, end, flag);
  375. isLeadingMargin = false;
  376. }
  377. if (quoteColor != defaultValue) {
  378. mBuilder.setSpan(new QuoteSpan(quoteColor), start, end, 0);
  379. quoteColor = defaultValue;
  380. }
  381. if (isBullet) {
  382. mBuilder.setSpan(new BulletSpan(gapWidth, bulletColor), start, end, 0);
  383. isBullet = false;
  384. }
  385. if (proportion != -1) {
  386. mBuilder.setSpan(new RelativeSizeSpan(proportion), start, end, flag);
  387. proportion = -1;
  388. }
  389. if (xProportion != -1) {
  390. mBuilder.setSpan(new ScaleXSpan(xProportion), start, end, flag);
  391. xProportion = -1;
  392. }
  393. if (isStrikethrough) {
  394. mBuilder.setSpan(new StrikethroughSpan(), start, end, flag);
  395. isStrikethrough = false;
  396. }
  397. if (isUnderline) {
  398. mBuilder.setSpan(new UnderlineSpan(), start, end, flag);
  399. isUnderline = false;
  400. }
  401. if (isSuperscript) {
  402. mBuilder.setSpan(new SuperscriptSpan(), start, end, flag);
  403. isSuperscript = false;
  404. }
  405. if (isSubscript) {
  406. mBuilder.setSpan(new SubscriptSpan(), start, end, flag);
  407. isSubscript = false;
  408. }
  409. if (isBold) {
  410. mBuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, flag);
  411. isBold = false;
  412. }
  413. if (isItalic) {
  414. mBuilder.setSpan(new StyleSpan(Typeface.ITALIC), start, end, flag);
  415. isItalic = false;
  416. }
  417. if (isBoldItalic) {
  418. mBuilder.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), start, end, flag);
  419. isBoldItalic = false;
  420. }
  421. if (fontFamily != null) {
  422. mBuilder.setSpan(new TypefaceSpan(fontFamily), start, end, flag);
  423. fontFamily = null;
  424. }
  425. if (align != null) {
  426. mBuilder.setSpan(new AlignmentSpan.Standard(align), start, end, flag);
  427. align = null;
  428. }
  429. if (imageIsBitmap || imageIsDrawable || imageIsUri || imageIsResourceId) {
  430. if (imageIsBitmap) {
  431. mBuilder.setSpan(new ImageSpan(Utils.INSTANCE.getContext(), bitmap), start, end, flag);
  432. bitmap = null;
  433. imageIsBitmap = false;
  434. } else if (imageIsDrawable) {
  435. mBuilder.setSpan(new ImageSpan(drawable), start, end, flag);
  436. drawable = null;
  437. imageIsDrawable = false;
  438. } else if (imageIsUri) {
  439. mBuilder.setSpan(new ImageSpan(Utils.INSTANCE.getContext(), uri), start, end, flag);
  440. uri = null;
  441. imageIsUri = false;
  442. } else {
  443. mBuilder.setSpan(new ImageSpan(Utils.INSTANCE.getContext(), resourceId), start, end, flag);
  444. resourceId = 0;
  445. imageIsResourceId = false;
  446. }
  447. }
  448. if (clickSpan != null) {
  449. mBuilder.setSpan(clickSpan, start, end, flag);
  450. clickSpan = null;
  451. }
  452. if (url != null) {
  453. mBuilder.setSpan(new URLSpan(url), start, end, flag);
  454. url = null;
  455. }
  456. if (isBlur) {
  457. mBuilder.setSpan(new MaskFilterSpan(new BlurMaskFilter(radius, style)), start, end, flag);
  458. isBlur = false;
  459. }
  460. flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
  461. }
  462. }
  463. }

三、参考

Android控件之TextView全解析