Android 使用 StaticLayout 实现字符串换行
30 Oct 2015
static class LongTextView extends View {
private static final float TEXT_SIZE = 60;
private static final CharSequence TEXT = "This is a long long long " +
"long long long long long long long long long long " +
"long long long string";
TextPaint textPaint;
public LongTextView(Context context) {
super(context);
textPaint = new TextPaint();
textPaint.setColor(Color.BLUE);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(TEXT_SIZE);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
StaticLayout staticLayout = new StaticLayout(
TEXT, textPaint,
canvas.getWidth(),
Layout.Alignment.ALIGN_NORMAL,
1.0f, 0.0f, false
);
staticLayout.draw(canvas);
}
}