android staticlayout文字繪製

來源:互聯網
上載者:User

標籤:字型   restore   過程   static   int   raw   size   imp   寬度   

  • StaticLayout。這個也是使用 Canvas 來進行文字的繪製,不過並不是使用 Canvas 的方法。

    Canvas.drawText() 只能繪製單行的文字,而不能換行。它:

    不能在 View 的邊緣自動折行
  • taticLayout 的構造方法是 StaticLayout(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad),其中參數裡:

    width 是文字地區的寬度,文字到達這個寬度後就會自動換行;
    align 是文字的對齊方向;
    spacingmult 是行間距的倍數,通常情況下填 1 就好;
    spacingadd 是行間距的額外增加值,通常情況下填 0 就好;
    includeadd 是指是否在文字上下添加額外的空間,來避免某些過高的字元的繪製出現越界。

    如果你需要進行多行文字的繪製,並且對文字的排列和樣式沒有太複雜的花式要求,那麼使用 StaticLayout 就好。
  •  TextPaint textPaint =new TextPaint();        textPaint.setTextSize(40);        //是否使用偽粗體 之所以叫偽粗體( fake bold ),因為它並不是通過選用更高 weight 的字型讓文字變粗,而是通過程式在運行時把文字給「描粗」了。        textPaint.setFakeBoldText(true);        //是否加底線        textPaint.setUnderlineText(true);        //是否加刪除線        textPaint.setStrikeThruText(true);        String text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";        StaticLayout staticLayout = new StaticLayout(text,textPaint,600, Layout.Alignment.ALIGN_NORMAL,                1,0,true);        String text2 = "a\nbc\ndefghi\njklm\nnopqrst\nuvwx\nyz";        StaticLayout staticLayout2 = new StaticLayout(text2, textPaint, 600,                Layout.Alignment.ALIGN_NORMAL, 1, 0, true);        canvas.save();        canvas.translate(50,100);        staticLayout.draw(canvas);        canvas.translate(0,200);        staticLayout2.draw(canvas);        canvas.restore();

      

android staticlayout文字繪製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.