日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JavaMe開發(fā):繪制可自動換行文本

【問題描述】

JavaMe Graphics類中的drawString不支持文本換行,這樣繪制比較長的字符串時,文本被繪制在同一行,超過屏幕部分的字符串被截斷了。如何使繪制的文本能自動換行呢?

【分析】

drawString無法實現(xiàn)自動換行,但可以實現(xiàn)文本繪制的定位。因此可考慮,將文本拆分為多個子串,再對子串進行繪制。拆分的策略如下:

1 遇到換行符,進行拆分;

2 當字符串長度大于設定的長度(一般為屏幕的寬度),進行拆分。

【步驟】

1 定義一個String和String []對象;

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

2 實現(xiàn)字符串自動換行拆分函數(shù)

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.     // 字符串切割,實現(xiàn)字符串自動換行  
  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
  70.         {  
  71.         result[loop] = (String)nodes.elementAt(loop);  
  72.         //System.out.println(result[loop]);  
  73.         }  
  74.  
  75.         }  
  76.  
  77.         return result;  
  78.         }  
  79. }  

3 調用拆分函數(shù),實現(xiàn)字符串的拆分

 
 
 
 
  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
  4. {  
  5.     graphics.drawString(info_wrap[i], 5, i * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);  

繪制的效果如圖1所示:

圖1 自動換行字符串繪制效果


網(wǎng)頁題目:JavaMe開發(fā):繪制可自動換行文本
網(wǎng)址分享:http://www.5511xx.com/article/dhhosgc.html