DBMNG数据库管理与应用

科学是实事求是的学问,来不得半点虚假。
当前位置:首页 > 移动应用 > JavaME

JavaMe开发:绘制可自动换行文本

【问题描述】

JavaMe Graphics类中的drawString不支持文本换行,这样绘制比较长的字符串时,文本被绘制在同一行,超过屏幕部分的字符串被截断了。如何使绘制的文本能自动换行呢?


【分析】

drawString无法实现自动换行,但可以实现文本绘制的定位。因此可考虑,将文本拆分为多个子串,再对子串进行绘制。拆分的策略如下:

1 遇到换行符,进行拆分;

2 当字符串长度大于设定的长度(一般为屏幕的宽度),进行拆分。


【步骤】

1 定义一个String和String []对象;

  1. private String info;  
  2. private String info_wrap[]; 

2 实现字符串自动换行拆分函数

StringDealMethod.java

  1. package com.token.util;  
  2.  
  3. import java.util.Vector;  
  4.  
  5. import javax.microedition.lcdui.Font;  
  6.  
  7. public class StringDealMethod {  
  8.     public StringDealMethod()  
  9.     {  
  10.           
  11.     }  
  12.  
  13.     // 字符串切割,实现字符串自动换行 
  14.     public static String[] format(String text, int maxWidth, Font ft) {  
  15.          String[] result = null;  
  16.          Vector tempR = new Vector();  
  17.          int lines = 0;  
  18.          int len = text.length();  
  19.          int index0 = 0;  
  20.          int index1 = 0;  
  21.          boolean wrap;  
  22.          while (true) {  
  23.           int widthes = 0;  
  24.           wrap = false;  
  25.           for (index0 = index1; index1 < len; index1++) {  
  26.            if (text.charAt(index1) == '\n') {  
  27.                 index1++;  
  28.                 wrap = true;  
  29.                 break;  
  30.                }  
  31.                widthes = ft.charWidth(text.charAt(index1)) + widthes;  
  32.  
  33.                if (widthes > maxWidth) {  
  34.                 break;  
  35.                }  
  36.               }  
  37.               lines++;  
  38.  
  39.               if (wrap) {  
  40.                tempR.addElement(text.substring(index0, index1 - 1));  
  41.               } else {  
  42.                tempR.addElement(text.substring(index0, index1));  
  43.               }  
  44.               if (index1 >= len) {  
  45.                break;  
  46.               }  
  47.              }  
  48.              result = new String[lines];  
  49.              tempR.copyInto(result);  
  50.              return result;  
  51.             }  
  52.       
  53.     public static String[] split(String original, String separator) {  
  54.         Vector nodes = new Vector();  
  55.         //System.out.println("split start..................."); 
  56.         //Parse nodes into vector 
  57.         int index = original.indexOf(separator);  
  58.         while(index>=0) {  
  59.         nodes.addElement( original.substring(0, index) );  
  60.         original = original.substring(index+separator.length());  
  61.         index = original.indexOf(separator);  
  62.         }  
  63.         // Get the last node 
  64.         nodes.addElement( original );  
  65.  
  66.         // Create splitted string array 
  67.         String[] result = new String[ nodes.size() ];  
  68.         if( nodes.size()>0 ) {  
  69.         for(int loop=0; loop<nodes.size(); loop++)  
  70.         {  
  71.         result[loop] = (String)nodes.elementAt(loop);  
  72.         //System.out.println(result[loop]); 
  73.         }  
  74.  
  75.         }  
  76.  
  77.         return result;  
  78.         }  
  79. }  

3 调用拆分函数,实现字符串的拆分

  1. int width = getWidth();  
  2.  
  3. Font ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);  
  4.           
  5. info = "欢迎使用!\n" 
  6.     +"1 MVC测试;\n" 
  7.     +"2 自动换行测试,绘制可自动识别换行的字符串。\n";  
  8. info_wrap = StringDealMethod.format(info, width-10, ft);  

4 绘制字符串

  1. graphics.setColor(Color.text);  
  2. graphics.setFont(ft);  
  3. for(int i=0; i<info_wrap.length; i++)  
  4. {  
  5.     graphics.drawString(info_wrap[i], 5, i * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);  

绘制的效果如图1所示:


图1 自动换行字符串绘制效果

原文链接:http://blog.csdn.net/tandesir/article/details/7541403

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号