Android 富文本编辑器 - ListItemSpan

import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.text.Spanned;
import android.text.style.LeadingMarginSpan;
import android.util.Log;

public class ListItemSpan implements LeadingMarginSpan {
  private final int mNumber;

  private static final int BULLET_RADIUS = 3;
  private static final int NUMBER_RADIUS = 10;

  //Gap should be about 1em
  public static final int STANDARD_GAP_WIDTH = 30;

  public ListItemSpan() {
      mNumber = -1;
  }

  public ListItemSpan(int number) {
      mNumber = number;
  }
  
  public int getNumber() {
    return this.mNumber;
  }

  public int getLeadingMargin(boolean first) {
    
    if (!first) {
      return 0;
    }
    
      if (mNumber != -1) {
          return 2 * NUMBER_RADIUS + STANDARD_GAP_WIDTH;
      } else {
          return 2 * BULLET_RADIUS + STANDARD_GAP_WIDTH;
      }
  }

  public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top,
                                int baseline, int bottom, CharSequence text, int start, int end,
                                boolean first, Layout l) {
    
      if (((Spanned) text).getSpanStart(this) == start) {
          Paint.Style style = p.getStyle();
          p.setStyle(Paint.Style.FILL);

          if (mNumber != -1) {
              c.drawText(mNumber + ".", 
                  x + dir, 
                  baseline, p);
              
          } else {
              c.drawText("\u2022", x + dir, baseline, p);
          }

          p.setStyle(style);
      }
  }
}


-- 源代码引用自 HtmlCleaner Android


======

最近在做Android上的富文本编辑器。

有朋友问到

Android 里面有内置的BulletSpan 

那带有数字的 number list

应该怎么做呢?


其实如果不会自己写Span

耐心Google 一定能得到相关代码

我借花献佛

把Html Cleaner的代码贴在这里

供大家参考


你可能感兴趣的:(android,富文本编辑器,RichTextEditor)