android 自动换行

package com.dcs.tools;

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 {

    privatefloat  mTextPosx = 0;// x坐标
    privatefloat  mTextPosy = 0;// y坐标
    privatefloat mTextWidth = 0;// 绘制宽度
    privatefloat mTextHeight = 0;// 绘制高度
    private intmFontHeight = 0;// 绘制字体高度
    private intmPageLineNum = 0;// 每一页显示的行数
    private intmCanvasBGColor = 0;// 背景颜色
    private intmFontColor = 0;// 字体颜色
    private intmAlpha = 0;// Alpha值
    private intmRealLine = 0;// 字符串真实的行数
    private intmCurrentLine = 0;// 当前行
    private intmTextSize = 0;// 字体大小
    privateString mStrText = "";
    privateVector<String> mString = null;
    privatePaint mPaint = null;

    publicTextUtil(String StrText, float  x,float  y, float w, float h, int bgcolor,
           int textcolor, int alpha, int textsize) {
       mPaint = new Paint();
       mString = new Vector<String>();
       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;
    }

    publicvoid 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 voidgetTextIfon() {
       char ch;
       int w = 0;
       int istart = 0;
       FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性
       mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);//获得字体高度
       mPageLineNum = (int) (mTextHeight / mFontHeight);// 获得行数
       
       int count = this.mStrText.length();
       for (int i = 0; i < count; i++) {
           ch = this.mStrText.charAt(i);
           float[] widths = new float[1];
           String str = String.valueOf(ch);
           mPaint.getTextWidths(str, widths);
           if (ch == '\n') {
               mRealLine++;// 真实的行数加一
               mString.addElement(this.mStrText.substring(istart, i));
               istart = i + 1;
               w = 0;
           } else {
               w += (int) Math.ceil(widths[0]);
               if (w > this.mTextWidth) {
                   mRealLine++;// 真实的行数加一
                   mString.addElement(this.mStrText.substring(istart, i));
                   istart = i;
                   i--;
                   w = 0;
               } else {
                   if (i == count - 1) {
                       mRealLine++;// 真实的行数加一
                       mString.addElement(this.mStrText.substring(istart,
                               count));
                   }
               }
           }
       }
    }

   
    public voiddrawText(Canvas canvas) {
       for (int i = this.mCurrentLine, j = 0; i <this.mRealLine; i++, j++) {
           if (j > this.mPageLineNum) {
               break;
           }
           canvas.drawText((String) (mString.elementAt(i)),this.mTextPosx,
                   this.mTextPosy + this.mFontHeight * j, mPaint);
       }
    }

   
    publicboolean keyDown(int keyCode, KeyEvent event) {
       if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
           if (this.mCurrentLine > 0) {
               this.mCurrentLine--;
           }
       } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
           if ((this.mCurrentLine + this.mPageLineNum) <(this.mRealLine - 1)) {
               this.mCurrentLine++;
           }
       }
       return false;
    }
}

你可能感兴趣的:(android 自动换行)