Canvas.DrawText自绘控制换行

Android DrawText字符串的绘制
文章分类:Java编程
Java代码
1. package com.Aina.Android;
2.
3. import java.util.Vector;
4.
5. import android.graphics.Canvas;
6. import android.graphics.Color;
7. import android.graphics.Paint;
8. import android.graphics.Paint.FontMetrics;
9. import android.view.KeyEvent;
10.
11. public class TextUtil {
12.
13. private int mTextPosx = 0;// x坐标
14. private int mTextPosy = 0;// y坐标
15. private int mTextWidth = 0;// 绘制宽度
16. private int mTextHeight = 0;// 绘制高度
17. private int mFontHeight = 0;// 绘制字体高度
18. private int mPageLineNum = 0;// 每一页显示的行数
19. private int mCanvasBGColor = 0;// 背景颜色
20. private int mFontColor = 0;// 字体颜色
21. private int mAlpha = 0;// Alpha值
22. private int mRealLine = 0;// 字符串真实的行数
23. private int mCurrentLine = 0;// 当前行
24. private int mTextSize = 0;// 字体大小
25. private String mStrText = "";
26. private Vector mString = null;
27. private Paint mPaint = null;
28.
29. public TextUtil(String StrText, int x, int y, int w, int h, int bgcolor,
30. int textcolor, int alpha, int textsize) {
31. mPaint = new Paint();
32. mString = new Vector();
33. this.mStrText = StrText;
34. this.mTextPosx = x;
35. this.mTextPosy = y;
36. this.mTextWidth = w;
37. this.mTextHeight = h;
38. this.mCanvasBGColor = bgcolor;
39. this.mFontColor = textcolor;
40. this.mAlpha = alpha;
41. this.mTextSize = textsize;
42. }
43.
44. public void InitText() {
45. mString.clear();// 清空Vector
46. // 对画笔属性的设置
47. // mPaint.setARGB(this.mAlpha, Color.red(this.mFontColor), Color
48. // .green(this.mFontColor), Color.blue(this.mFontColor));
49. mPaint.setTextSize(this.mTextSize);
50. mPaint.setColor(Color.BLUE);
51. this.GetTextIfon();
52. }
53.
54. /**
55. * 得到字符串信息包括行数,页数等信息
56. */
57. public void GetTextIfon() {
58. char ch;
59. int w = 0;
60. int istart = 0;
61. FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性
62. mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 获得字体高度
63. mPageLineNum = mTextHeight / mFontHeight;// 获得行数
64. int count = this.mStrText.length();
65. for (int i = 0; i < count; i++) {
66. ch = this.mStrText.charAt(i);
67. float[] widths = new float[1];
68. String str = String.valueOf(ch);
69. mPaint.getTextWidths(str, widths);
70. if (ch == '\n') {
71. mRealLine++;// 真实的行数加一
72. mString.addElement(this.mStrText.substring(istart, i));
73. istart = i + 1;
74. w = 0;
75. } else {
76. w += (int) Math.ceil(widths[0]);
77. if (w > this.mTextWidth) {
78. mRealLine++;// 真实的行数加一
79. mString.addElement(this.mStrText.substring(istart, i));
80. istart = i;
81. i--;
82. w = 0;
83. } else {
84. if (i == count - 1) {
85. mRealLine++;// 真实的行数加一
86. mString.addElement(this.mStrText.substring(istart,
87. count));
88. }
89. }
90. }
91. }
92. }
93.
94. /**
95. * 绘制字符串
96. *
97. * @param canvas
98. */
99. public void DrawText(Canvas canvas) {
100. for (int i = this.mCurrentLine, j = 0; i < this.mRealLine; i++, j++) {
101. if (j > this.mPageLineNum) {
102. break;
103. }
104. canvas.drawText((String) (mString.elementAt(i)), this.mTextPosx,
105. this.mTextPosy + this.mFontHeight * j, mPaint);
106. }
107. }
108. /**
109. * 翻页等按键处理
110. * @param keyCode
111. * @param event
112. * @return
113. */
114. public boolean KeyDown(int keyCode, KeyEvent event)
115. {
116. if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
117. {
118. if (this.mCurrentLine > 0)
119. {
120. this.mCurrentLine--;
121. }
122. }
123. else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
124. {
125. if ((this.mCurrentLine + this.mPageLineNum) < (this.mRealLine - 1))
126. {
127. this.mCurrentLine++;
128. }
129. }
130. return false;
131. }
132. }

Java代码
1. package com.Aina.Android;
2.
3. import android.content.Context;
4. import android.graphics.Canvas;
5. import android.graphics.Color;
6. import android.graphics.Paint;
7. import android.view.KeyEvent;
8. import android.view.View;
9.
10. public class MyView extends View implements Runnable{
11.
12. private Paint mPaint;
13. private int mICount = 0;
14. private TextUtil mTextUtil;
15.
16. public MyView(Context context) {
17. super(context);
18. mPaint = new Paint();
19. String string = "测试自动换行(第1行)\n\n测试自动换行(第2行)\n测试自动换行(第3行)\n测试自动换行(第4行)\n测试自动换行(第5行)\n测试自动换行(第6行)\n测试自动换行(第7行)";
20. mTextUtil = new TextUtil(string, 15, 150, 300, 80, 0x000000, 0xff00ff,
21. 255, 16);
22. mTextUtil.InitText();
23. new Thread(this).start();
24. }
25.
26. @Override
27. protected void onDraw(Canvas canvas) {
28. super.onDraw(canvas);
29. canvas.drawColor(Color.BLACK);
30. mPaint.setAntiAlias(true);
31. if (mICount < 100) {
32. mICount++;
33. }
34. mPaint.setColor(Color.RED);
35. canvas.drawText("装在进度:" + mICount + "%......", 10, 20, mPaint);
36. mTextUtil.DrawText(canvas);
37. }
38.
39. @Override
40. public void run() {
41. Thread.currentThread();
42. while(!Thread.interrupted()){
43. try{
44. Thread.sleep(10);
45. }catch(Exception ex){
46. ex.printStackTrace();
47. Thread.currentThread().interrupt();
48. }
49. this.postInvalidate();
50. }
51. }
52. public boolean onKeyDown(int keyCode, KeyEvent event) {
53.
54. return mTextUtil.KeyDown(keyCode, event);
55. }
56. }

Java代码
1. package com.Aina.Android;
2.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.view.KeyEvent;
6.
7. public class Test_DrawText extends Activity {
8. /** Called when the activity is first created. */
9. private MyView mv ;
10. @Override
11. public void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. mv = new MyView(this);
14. setContentView(mv);
15. }
16.
17. @Override
18. public boolean onKeyDown(int keyCode, KeyEvent event) {
19. mv.onKeyDown(keyCode, event);
20. return super.onKeyDown(keyCode, event);
21. }
22.
23. }
Android绘制文本对象FontMetrics介绍
时间:2011-01-13 14:29 来源:未知 作者:3g开发者课堂 点击: 170次
Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标。 它的思路和java.awt.FontMetrics的基本相同。
FontMetrics对象
它以四个基本坐标为基准,分别为:
・FontMetrics.top
・FontMetrics.ascent
・FontMetrics.descent
・FontMetrics.bottom


实例JAVA代码如下:
Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize( 35);
textPaint.setColor( Color.WHITE);

// FontMetrics对象
FontMetrics fontMetrics = textPaint.getFontMetrics();
String text = "abcdefghijklmnopqrstu";

// 计算每一个坐标
float baseX = 0;
float baseY = 100;
float topY = baseY + fontMetrics.top;
float ascentY = baseY + fontMetrics.ascent;
float descentY = baseY + fontMetrics.descent;
float bottomY = baseY + fontMetrics.bottom;

// 绘制文本
canvas.drawText( text, baseX, baseY, textPaint);

// BaseLine描画
Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);>
baseLinePaint.setColor( Color.RED);
canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);

// Base描画
canvas.drawCircle( baseX, baseY, 5, baseLinePaint);

// TopLine描画
Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
topLinePaint.setColor( Color.LTGRAY);
canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);

// AscentLine描画
Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
ascentLinePaint.setColor( Color.GREEN);
canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);

// DescentLine描画
Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
descentLinePaint.setColor( Color.YELLOW);
canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);

// ButtomLine描画
Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
bottomLinePaint.setColor( Color.MAGENTA);
canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);

你可能感兴趣的:(java,String,vector,测试,import,float)