展会信息港展会大全

Android教程之显示文字
来源:互联网   发布日期:2015-10-02 16:08:54   浏览:2129次  

导读:下面的代码讲的是用画文字的方法,来让文章换行。其中最主要的是定义文字的坐标,在每一页显示文字多少行。主要的功能就不在XML里写了,而是在main下面写好。剩下的像字体大� ⒀丈?⒒嬷聘吆涂恚?阕约憾ㄒ寰秃�......

下面的代码讲的是用画文字的方法,来让文章换行。其中最主要的是定义文字的坐标,在每一页显示文字多少行。主要的功能就不在XML里写了,而是在main下面写好。剩下的像字体大孝颜色、绘制高和宽,你自己定义就好了。下面我们就来看看用画的方法来让文章自动换行。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

package android.study;

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);// 获得字体高度

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));

}

}

}

}

}

/**

* 绘制字符串

*

* @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;

}

}

赞助本站

人工智能实验室

相关热词: Android 教程 显示文字

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港