Android bitmap绘制文字自动换行

public Bitmap getNewBitMap(String text) {
        Bitmap newBitmap = Bitmap.createBitmap(120,150, Config.ARGB_4444);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(bmp, 0, 0, null);
        TextPaint textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(16.0F);
        StaticLayout sl= new StaticLayout(text, textPaint, newBitmap.getWidth()-8, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
        canvas.translate(6, 40);
        sl.draw(canvas);
        return newBitmap;
    }

Android StaticLayout参数解释

 

StaticLayout layout = new StaticLayout(context.getString(R.string.about),textPaint,(int)(300*fDensity),Alignment.ALIGN_CENTER,1.5F,0,false);

layout.draw(canvas);

参数含义:

1.字符串子资源

2 .画笔对象

3.layout的宽度,字符串超出宽度时自动换行。

4.layout的样式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE  三种。

5.相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度。

6.相对行间距,0表示0个像素。

实际行间距等于这两者的和。

7.还不知道是什么意思,参数名是boolean includepad。

需要指出的是这个layout是默认画在Canvas的(0,0)点的,如果需要调整位置只能在draw之前移Canvas的起始坐标
canvas.translate(x,y);

 

posted @ 2016-11-02 15:47  星辰之力  阅读(1774)  评论(0编辑  收藏  举报