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 {
private float mTextPosx = 0;// x坐標(biāo)
private float mTextPosy = 0;// y坐標(biāo)
private float mTextWidth = 0;// 繪制寬度
private float mTextHeight = 0;// 繪制高度
private int mFontHeight = 0;// 繪制字體高度
private int mPageLineNum = 0;// 每一頁顯示的行數(shù)
private int mCanvasBGColor = 0;// 背景顏色
private int mFontColor = 0;// 字體顏色
private int mAlpha = 0;// Alpha值
private int mRealLine = 0;// 字符串真實的行數(shù)
private int mCurrentLine = 0;// 當(dāng)前行
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
// 對畫筆屬性的設(shè)置
// 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();
}
/**
* 得到字符串信息包括行數(shù),頁數(shù)等信息
*/
public void GetTextIfon() {
char ch;
int w = 0;
int istart = 0;
FontMetrics fm = mPaint.getFontMetrics();// 得到系統(tǒng)默認(rèn)字體屬性
mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 獲得字體高度
mPageLineNum = (int) (mTextHeight / mFontHeight);// 獲得行數(shù)
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++;// 真實的行數(shù)加一
mString.addElement(this.mStrText.substring(istart, i));
istart = i + 1;
w = 0;
} else {
w += (int) Math.ceil(widths[0]);
if (w > this.mTextWidth) {
mRealLine++;// 真實的行數(shù)加一
mString.addElement(this.mStrText.substring(istart, i));
istart = i;
i--;
w = 0;
} else {
if (i == count - 1) {
mRealLine++;// 真實的行數(shù)加一
mString.addElement(this.mStrText.substring(istart,
count));
}
}
}
}
}
/**
* 繪制字符串
*
* @param canvas
*/
public void DrawText(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);
}
}
/**
* 翻頁等按鍵處理
*
* @param keyCode
* @param event
* @return
*/
public boolean 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;
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。