android staticlayout文字绘制

  • 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();
    

      

posted on 2018-09-05 15:01  endian11  阅读(3150)  评论(0编辑  收藏  举报

导航