[J2me]长文本自动断行--TextScrollFrame

package lab.sodino.wear; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; public class MyCanvas extends Canvas { private TextScroll textScroll; private String text = "abcdefghijklmnopqrstuvwxy0123456789为情伤,世间事皆无常。笑沧桑,成行泪化寒窗。勿彷徨,脱素裹着春装,忆流芳。"; public MyCanvas() { setFullScreenMode(true); text += text += text += text += text += text; textScroll = new TextScroll(40, 60, 160, 200); textScroll.setFontAndText(Font.getDefaultFont(), text); textScroll.setTextColor(0xffff00); } protected void paint(Graphics g) { g.setColor(0); g.fillRect(0, 0, getWidth(), getHeight()); textScroll.renderScrollFrame(g); } protected void keyReleased(int key) { super.keyReleased(key); if (key == -1 || key == Canvas.KEY_NUM2) { // up textScroll.modifyFirstDrawLineNum(-1); repaint(); } else if (key == -2 || key == Canvas.KEY_NUM8) { // down textScroll.modifyFirstDrawLineNum(1); repaint(); } } }

 

package lab.sodino.wear; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; public class TextScroll { private String text; private int leftX; private int topY; private int frameWidth; private int frameHeight; private Font font; private int showLineCount; private int textColor; private int parentWidth; private int parentHeight; private int firstDrawLineNum; private int allLineCount; public TextScroll(int leftX, int topY, int width, int height) { setStartPoint(leftX, topY); setSize(width, height); setFontAndText(Font.getDefaultFont(), null); firstDrawLineNum = 0; } public void renderScrollFrame(Graphics g) { if (text != null && text.length() > 0) { // setClipArea /* start:Draw a test background */ g.setColor(0xffffff); g.fillRect(leftX, topY, frameWidth, frameHeight); /* end:Draw a test background */ g.setClip(leftX, topY, frameWidth, frameHeight); g.setColor(0x0000ff); // g.setColor(textColor); // drawText int centerX = leftX + (frameWidth >> 1); int fontHeigth = font.getHeight(); String lineStr = ""; int nextLineCharIdx = 0; int lineIdx = 0; while ((lineStr = getLine(font, text, nextLineCharIdx)).length() > 0) { int cellY = topY + (lineIdx - firstDrawLineNum) * fontHeigth; g.drawString(lineStr, centerX, cellY, Graphics.HCENTER | Graphics.TOP); nextLineCharIdx += lineStr.length(); lineIdx++; } // resume clip area g.setClip(0, 0, parentWidth, parentHeight); } } private String getLine(Font f, String text, int startIdx) { String line = ""; if (startIdx >= text.length()) { return line; } while (startIdx < text.length()) { String tempStr = line + text.charAt(startIdx); if (f.stringWidth(tempStr) > frameWidth) { break; } else { line = tempStr; startIdx++; } } return line; } public void setFontAndText(Font f, String text) { font = f; this.text = text; showLineCount = (int) Math.ceil(1.0 * frameHeight / font.getHeight()); allLineCount = calculateAllLineCount(f, text); System.out.println("TextScroll.setFontAndText() allLineCount = " + allLineCount); } private int calculateAllLineCount(Font f, String text) { int count = 0; if (f == null || text == null || text.length() == 0) { return count; } String lineStr = ""; int nextLineCharIdx = 0; int lineIdx = 0; while ((lineStr = getLine(font, text, nextLineCharIdx)).length() > 0) { nextLineCharIdx += lineStr.length(); lineIdx++; } count = lineIdx; return count; } public void setStartPoint(int leftX, int topY) { this.leftX = leftX; this.topY = topY; } public int getLeftX() { return leftX; } public int getTopY() { return topY; } public String getText() { return text; } public void setSize(int width, int height) { this.frameWidth = width; this.frameHeight = height; } public int getWidth() { return frameWidth; } public int getHeight() { return frameHeight; } public void setTextColor(int color) { textColor = color; } public int getTextColor() { return textColor; } public void setParentSize(int parentW, int parentH) { this.parentWidth = parentW; this.parentHeight = parentH; } public int modifyFirstDrawLineNum(int variation) { if (variation == -1) { if (firstDrawLineNum != 0) { firstDrawLineNum += variation; } else { System.out.println("the top"); } } else if (variation == 1) { if (firstDrawLineNum + showLineCount <= allLineCount) { firstDrawLineNum += variation; } else { System.out.println("the buttom"); } } return firstDrawLineNum; } }

 

附上另外一个自动断行并将文字转化为Image的一个算法:

 

public static Image createScrollTextImage(String text, Font font, int color, int width) { Image scrollText = null; if (text == null || font == null || width < font.charWidth('_')) { return null; } else { Vector strVec = new Vector(); int parseIdx = 0; loop1: while (parseIdx < text.length()) { String line = ""; loop2: while (true) { String temp = line + text.charAt(parseIdx); if (font.stringWidth(temp) < width) { line = temp; parseIdx++; if (parseIdx == text.length()) { if (line.length() != 0) { strVec.addElement(line); } break loop1; } } else { strVec.addElement(line); break loop2; } } } int lineCount = strVec.size(); int fontHeight = font.getHeight(); int height = lineCount * fontHeight; int abandonColor = ABANDON_COLOR_A; if (color == ABANDON_COLOR_A) { abandonColor = ABANDON_COLOR_B; } Image img = Image.createImage(width, height); Graphics g = img.getGraphics(); g.setColor(abandonColor); g.fillRect(0, 0, width, height); g.setColor(color); for (int i = 0; i < lineCount; i++) { g.drawString(String.valueOf(strVec.elementAt(i)), 0, i * fontHeight); } int[] rgbArr = img.getRGB(); g = null; img = null; for (int i = 0; i < rgbArr.length; i++) { if (rgbArr[i] == abandonColor) { rgbArr[i] = rgbArr[i] & 0x00ffffff; } } img = Image.createImage(rgbArr, width, height); scrollText = img; } return scrollText; }

你可能感兴趣的:(image,String,vector,null,import,j2me)