Android Canvas绘制文字 自动换行

import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.view.KeyEvent;

public class TextUtil {

   private float  mTextPosx = 0;// x坐标
   private float  mTextPosy = 0;// y坐标
   private float mTextWidth = 0;// 绘制宽度
   private float mTextHeight = 0;// 绘制高度
   private int mFontHeight = 0;// 绘制字体高度
   private int mPageLineNum = 0;// 每一页显示的行数
   private int mCanvasBGColor = 0;// 背景颜色
   private int mFontColor = 0;// 字体颜色
   private int mAlpha = 0;// Alpha值
   private int mRealLine = 0;// 字符串真实的行数
   private int mCurrentLine = 0;// 当前行
   private int mTextSize = 0;// 字体大小
   private String mStrText = "";
   private Vector mString = null;
   private Paint mPaint = null;

   public TextUtil(String StrText, float  x, float  y, float w, float h, int bgcolor,
           int textcolor, int alpha, int textsize) {
       mPaint = new Paint();
       mString = new Vector();
       this.mStrText = StrText;
       this.mTextPosx = x;
       this.mTextPosy = y;
       this.mTextWidth = w;
       this.mTextHeight = h;
       this.mCanvasBGColor = bgcolor;
       this.mFontColor = textcolor;
       this.mAlpha = alpha;
       this.mTextSize = textsize;
   }

   public void InitText() {
       mString.clear();// 清空Vector
       // 对画笔属性的设置
       // mPaint.setARGB(this.mAlpha, Color.red(this.mFontColor), Color
       // .green(this.mFontColor), Color.blue(this.mFontColor));
       mPaint.setTextSize(this.mTextSize);
       mPaint.setColor(Color.BLUE);
       mPaint.setAntiAlias(true);
       
       this.GetTextIfon();
   }

   
   public void GetTextIfon() {
       char ch;
       int w = 0;
       int istart = 0;
       FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性
       mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 获得字体高度

你可能感兴趣的:(android基础)