段落級Span解析

1 簡介

之前已經講過TextView的基礎知識,現在在這進一步進行講解,這篇文字主要講解如何給TextView設置段落級別的Span。如果一個Span想要影響段落層次的文本格式,則需要實現ParagraphStyle。


2 ParagraphStyle

ParagraphStyle是一個接口,通過查看Android源碼,我們發現這個接口里面什么方法也沒有定義,因此,我們可以認為,這個接口無非是標識實現這個接口的Span為段落級別的Span。
在Android源碼中又繼續定義了幾個接口實現了ParagraphStyle接口。


ParagraphStyle
  1. LeadingMarginSpan:用來處理像word中項目符號一樣的接口;
  2. AlignmentSpan:用來處理整個段落對其的接口;
  3. LineBackgroundSpan:用來處理一行的背景的接口;
  4. LineHeightSpan:用來處理一行高度的接口;
  5. TabStopSpan:用來將字符串中的"\t"替換成相應的空行;

3 LeadingMarginSpan

LeadingMarginSpan用來控制整個段落左邊或者右邊顯示某些特定效果,里面有兩個接口方法。

/**
 * Returns the amount by which to adjust the leading margin. Positive values
 * move away from the leading edge of the paragraph, negative values move
 * towards it.
 * 
 * @param first true if the request is for the first line of a paragraph,
 * false for subsequent lines
 * @return the offset for the margin.
 */
public int getLeadingMargin(boolean first);
/**
 * Renders the leading margin.  This is called before the margin has been
 * adjusted by the value returned by {@link #getLeadingMargin(boolean)}.
 * 
 * @param c the canvas
 * @param p the paint. The this should be left unchanged on exit.
 * @param x the current position of the margin
 * @param dir the base direction of the paragraph; if negative, the margin
 * is to the right of the text, otherwise it is to the left.
 * @param top the top of the line
 * @param baseline the baseline of the line
 * @param bottom the bottom of the line
 * @param text the text
 * @param start the start of the line
 * @param end the end of the line
 * @param first true if this is the first line of its paragraph
 * @param layout the layout containing this line
 */
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 layout);

LeadingMarginSpan2還多規定了一個方法。

/**
 * Returns the number of lines of the paragraph to which this object is
 * attached that the "first line" margin will apply to.
 */
public int getLeadingMarginLineCount();

第一個方法first為是否為第一行,返回值為整個段落偏移的距離。
第二個方法可以在偏移的位置里面進行各種效果繪制。
第三個方法可以控制影響的行數。
下面通過三個LeadingMarginSpan的實現來具體說明。


3.1 BulletSpan

先來看BulletSpan實現的效果,效果如下圖所示:


BulletSpan

通過上面的圖片可以看見整個段落右移了一段距離,然后在移動留下的空間處繪制了一個小圓點。
具體來看代碼,BulletSpan代碼如下所示:

public int getLeadingMargin(boolean first) {
    return 2 * BULLET_RADIUS + mGapWidth;
}

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();
        int oldcolor = 0;

        if (mWantColor) {
            oldcolor = p.getColor();
            p.setColor(mColor);
        }

        p.setStyle(Paint.Style.FILL);

        if (c.isHardwareAccelerated()) {
            if (sBulletPath == null) {
                sBulletPath = new Path();
                // Bullet is slightly better to avoid aliasing artifacts on mdpi devices.
                sBulletPath.addCircle(0.0f, 0.0f, 1.2f * BULLET_RADIUS, Direction.CW);
            }

            c.save();
            c.translate(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f);
            c.drawPath(sBulletPath, p);
            c.restore();
        } else {
            c.drawCircle(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f, BULLET_RADIUS, p);
        }

        if (mWantColor) {
            p.setColor(oldcolor);
        }

        p.setStyle(style);
    }
}

第一個方法無論是否是第一行都返回了偏移距離為2 * BULLET_RADIUS + mGapWidth,因此整個段落都移動了相應的距離。
第二個方法繪制了一個圓形,((Spanned) text).getSpanStart(this) == start判斷了這一行的起始位置是否是整個Span的起始位置,如果是則繪制圓形,如果把這個判斷去掉,那么每一行都將繪制小圓形。


3.2 QuoteSpan

先看實現的效果,實現的效果如下所示:


QuoteSpan

QuoteSpan代碼如下所示:

public int getLeadingMargin(boolean first) {
    return STRIPE_WIDTH + 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 layout) {
    Paint.Style style = p.getStyle();
    int color = p.getColor();

    p.setStyle(Paint.Style.FILL);
    p.setColor(mColor);

    c.drawRect(x, top, x + dir * STRIPE_WIDTH, bottom, p);

    p.setStyle(style);
    p.setColor(color);
}

上面的代碼就十分清晰了,每行都偏移相應距離,然后每行都繪制矩形,就連成了一條豎線。


3.3 TextRoundSpan

如果希望做到兩端文字環繞圖片的效果,其實可以考慮編寫Span實現LeadingMarginSpan2。具體做法其實比較簡單,相對布局中放置ImageView和TextView,然后根據ImageView的大小計算TextView需要偏移的距離和行數,整個效果就可以實現,實現的效果如下所示:


TextRoundSpan
float fontSpacing=mTextView.getPaint().getFontSpacing();
lines = (int) (finalHeight/fontSpacing);
/**
 * Build the layout with LeadingMarginSpan2
 */
TextRoundSpan span = new TextRoundSpan(lines, finalWidth +10 );
class TextRoundSpan implements LeadingMarginSpan.LeadingMarginSpan2 {
  private int margin;
  private int lines;

  TextRoundSpan(int lines, int margin) {
      this.margin = margin;
      this.lines = lines;
  }

  /**
   * Apply the margin
   *
   * @param first
   * @return
   */
  @Override
  public int getLeadingMargin(boolean first) {
      if (first) {
          return margin;
      } else {
          return 0;
      }
  }

  @Override
  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 layout) {}


  @Override
  public int getLeadingMarginLineCount() {
      return lines;
  }
};

其實分析上面可以得出當當前行數小于等于getLeadingMarginLineCount(),getLeadingMargin(boolean first)中first的值為true。


4 AlignmentSpan

AlignmentSpan處理整個段落文字排列,當設置不同的排列方式,顯示的效果不同。


AlignmentSpan

AlignmentSpan接口中定義了一個接口方法,里面還有個Standard實現。

Layout.Alignment getAlignment();

AlignmentSpan比較簡單,不多做講述。


5 LineBackgroundSpan

LineBackgroundSpan用來設置每一行的背景顏色,這個和對字體設置顏色不同,具體區別如下:


TextBackgroundSpan

LineBackgroundSpan

可以看見下面圖片中背景顏色是整行的。
具體代碼如下:

public class MainActivity extends Activity {

    private static class MySpan implements LineBackgroundSpan {
        private final int color;

        public MySpan(int color) {
            this.color = color;
        }

        @Override
        public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int lnum) {
            final int paintColor = p.getColor();
            p.setColor(color);
            c.drawRect(new Rect(left, top, right, bottom), p);
            p.setColor(paintColor);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final TextView tv = new TextView(this);
        setContentView(tv);

        tv.setText("Lines:\n", TextView.BufferType.EDITABLE);
        appendLine(tv.getEditableText(), "123456 123 12345678\n", Color.BLACK);
        appendLine(tv.getEditableText(), "123456 123 12345678\n", Color.RED);
        appendLine(tv.getEditableText(), "123456 123 12345678\n", Color.BLACK);
    }

    private void appendLine(Editable text, String string, int color) {
        final int start = text.length();
        text.append(string);
        final int end = text.length();
        text.setSpan(new MySpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

6 LineHeightSpan

要想熟練使用這個Span,需要對字體的高度設置有著較好的理解。


LineHeight

Top和Ascent之間存在的距離是考慮到了類似讀音符號。Android依然會在繪制文本的時候在文本外層留出一定的邊距,這就是為什么top和bottom總會比ascent和descent大一點的原因。而在TextView中我們可以通過xml設置其屬性android:includeFontPadding="false"去掉一定的邊距值但是不能完全去掉。


LineHeightDemo

上面圖片的一行文字打印FontMetrics相應的值,如下所示:
  1. ascent:-46.38672
  2. top:-52.807617
  3. leading:0.0
  4. descent:12.207031
  5. bottom:13.549805

下面我們來看一下Android提供的DrawableMarginSpan的源碼。

public class DrawableMarginSpan
implements LeadingMarginSpan, LineHeightSpan
{
    public DrawableMarginSpan(Drawable b) {
        mDrawable = b;
    }

    public DrawableMarginSpan(Drawable b, int pad) {
        mDrawable = b;
        mPad = pad;
    }

    public int getLeadingMargin(boolean first) {
        return mDrawable.getIntrinsicWidth() + mPad;
    }

    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 layout) {
        int st = ((Spanned) text).getSpanStart(this);
        int ix = (int)x;
        int itop = (int)layout.getLineTop(layout.getLineForOffset(st));

        int dw = mDrawable.getIntrinsicWidth();
        int dh = mDrawable.getIntrinsicHeight();

        // XXX What to do about Paint?
        mDrawable.setBounds(ix, itop, ix+dw, itop+dh);
        mDrawable.draw(c);
    }

    public void chooseHeight(CharSequence text, int start, int end,
                             int istartv, int v,
                             Paint.FontMetricsInt fm) {
        if (end == ((Spanned) text).getSpanEnd(this)) {
            int ht = mDrawable.getIntrinsicHeight();

            int need = ht - (v + fm.descent - fm.ascent - istartv);
            if (need > 0)
                fm.descent += need;

            need = ht - (v + fm.bottom - fm.top - istartv);
            if (need > 0)
                fm.bottom += need;
        }
    }

    private Drawable mDrawable;
    private int mPad;
}
DrawableMarginSpan

這個Span實現了LeadingMarginSpan和LineHeightSpan接口,實現了LeadingMarginSpan接口是為了實現段落便宜的效果,不過這里的代碼存在一定的問題,因為會多次調用Drawable的繪制。實現LineHeightSpan是為了解決TextView高度的問題,設置最后一行的高度從而來保證整個TextView的高度大于或者等于Drawable的高度。

int need = ht - (v + fm.descent - fm.ascent - istartv);

上面v為這一行的起始垂直坐標,descent為正數,ascent為負數,istartv為整個Span的起始垂直坐標,上面表達式減去的就是整個TextView到這一行的高度,然后將這個高度和Drawable的高度進行對比,從而進行相應設置。


7 TabStopSpan

TabStopSpan用來將字符串中的"\t"替換成相應的空行,普通情況下"\t"不會進行顯示,當使用TabStopSpan可以將"\t"替換成相應長度的空白區域。


TabStopSpan
/**
 * Returns the offset of the tab stop from the leading margin of the
 * line.
 * @return the offset
 */
public int getTabStop();

這個接口方法返回空白的長度。

8 相關鏈接

Textview圖文基礎
段落級span
字符級span
自定義span

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,362評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,577評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,486評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,852評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,600評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,944評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,944評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,108評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,652評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,385評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,616評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,111評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,798評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,205評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,537評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,334評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,570評論 2 379

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,752評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,828評論 18 139
  • 開元十四年。李白懷著”仗劍去國辭親遠游”之情。李白去到了荊門山下。仿佛我也身臨其境。作者乘船遠渡到荊門外來到了楚國...
    欣怡然閱讀 4,238評論 0 0
  • 狀元鄉原來不叫狀元鄉,叫王家溝,很窮的一個小山村。由于這個村在宋唐時期出過兩個狀元,村領導為了招商引資,就把歷史翻...
    江志強閱讀 397評論 0 1