Paint和TextPaint,不能直接让文字换行。需要其他额外操作。
代码:
FontView
package com.chen.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
public class FontView extends View {
//文字内容
private static final String TEXT = "没有设置字体属性:哈123哈abc嗯";
private static final String TEXT_1 = "DEFAULT:哈123哈哈abc嗯";
private static final String TEXT_2 = "DEFAULT_BOLD:哈123哈abc嗯";
private static final String TEXT_3 = "SANS_SERIF:哈123哈abc嗯";
private static final String TEXT_4 = "SERIF:哈123哈abc嗯";
private static final String TEXT_5 = "MONOSPACE:哈123哈abc嗯";
//文字画笔
private TextPaint textPaint;
private TextPaint textPaint_1;
private TextPaint textPaint_2;
private TextPaint textPaint_3;
private TextPaint textPaint_4;
private TextPaint textPaint_5;
private int baseX, baseY;
public FontView(Context context) {
this(context, null);
}
public FontView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public FontView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint(context);
}
private void initPaint(Context context) {
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(70);
/**
* 设置文本的对其方式,可供选的方式有三种:CENTER,LEFT和RIGHT
*
* 如果选择LEFT,从指定位置开始,往右绘制文字。即:文字从指定绘制位置起始位置开始,左对齐
* 如果选择RIGHT,从指定位置开始,往左绘制文字。即:文字从指定绘制位置起始位置开始,右对齐
* 如果选择CENTER,从指定位置开始,往左右两边绘制文字。即:文字从指定绘制位置起始位置开始,文字中间对齐
*/
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setColor(Color.BLACK);
textPaint_1 = new TextPaint(textPaint);
textPaint_1.setTypeface(Typeface.DEFAULT);
textPaint_2 = new TextPaint(textPaint);
textPaint_2.setTypeface(Typeface.DEFAULT_BOLD);
textPaint_3 = new TextPaint(textPaint);
//The NORMAL style of the default sans serif typeface
textPaint_3.setTypeface(Typeface.SANS_SERIF);
// The NORMAL style of the default serif typeface
textPaint_4 = new TextPaint(textPaint);
textPaint_4.setTypeface(Typeface.SERIF);
//The NORMAL style of the default monospace typeface
textPaint_5 = new TextPaint(textPaint);
textPaint_5.setTypeface(Typeface.MONOSPACE);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
baseX = w / 2;
baseY = h / 2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/*
* @param text The text to be drawn
* @param x The x-coordinate of the origin of the text being drawn
* @param y The y-coordinate of the baseline of the text being drawn
* @param paint The paint used for the text (e.g. color, size, style)
*/
canvas.drawText(TEXT, baseX, baseY / 6, textPaint);
canvas.drawText(TEXT_1, baseX, baseY / 3, textPaint_1);
canvas.drawText(TEXT_2, baseX, baseY / 2, textPaint_2);
canvas.drawText(TEXT_3, baseX, baseY / 3 * 2, textPaint_3);
canvas.drawText(TEXT_4, baseX, baseY / 6 * 5, textPaint_4);
canvas.drawText(TEXT_5, baseX, baseY, textPaint_5);
}
}
布局代码中使用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.chen.view.FontView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>