這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)JavaMe開(kāi)發(fā)中怎么繪制可自動(dòng)換行文本,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)是專(zhuān)業(yè)的漢臺(tái)網(wǎng)站建設(shè)公司,漢臺(tái)接單;提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行漢臺(tái)網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
【問(wèn)題描述】
JavaMe Graphics類(lèi)中的drawString不支持文本換行,這樣繪制比較長(zhǎng)的字符串時(shí),文本被繪制在同一行,超過(guò)屏幕部分的字符串被截?cái)嗔?。如何使繪制的文本能自動(dòng)換行呢?
【分析】
drawString無(wú)法實(shí)現(xiàn)自動(dòng)換行,但可以實(shí)現(xiàn)文本繪制的定位。因此可考慮,將文本拆分為多個(gè)子串,再對(duì)子串進(jìn)行繪制。拆分的策略如下:
1 遇到換行符,進(jìn)行拆分;
2 當(dāng)字符串長(zhǎng)度大于設(shè)定的長(zhǎng)度(一般為屏幕的寬度),進(jìn)行拆分。
【步驟】
1 定義一個(gè)String和String []對(duì)象;
private String info; private String info_wrap[];
2 實(shí)現(xiàn)字符串自動(dòng)換行拆分函數(shù)
StringDealMethod.java
package com.token.util; import java.util.Vector; import javax.microedition.lcdui.Font; public class StringDealMethod { public StringDealMethod() { } // 字符串切割,實(shí)現(xiàn)字符串自動(dòng)換行 public static String[] format(String text, int maxWidth, Font ft) { String[] result = null; Vector tempR = new Vector(); int lines = 0; int len = text.length(); int index0 = 0; int index1 = 0; boolean wrap; while (true) { int widthes = 0; wrap = false; for (index0 = index1; index1 < len; index1++) { if (text.charAt(index1) == '\n') { index1++; wrap = true; break; } widthes = ft.charWidth(text.charAt(index1)) + widthes; if (widthes > maxWidth) { break; } } lines++; if (wrap) { tempR.addElement(text.substring(index0, index1 - 1)); } else { tempR.addElement(text.substring(index0, index1)); } if (index1 >= len) { break; } } result = new String[lines]; tempR.copyInto(result); return result; } public static String[] split(String original, String separator) { Vector nodes = new Vector(); //System.out.println("split start..................."); //Parse nodes into vector int index = original.indexOf(separator); while(index>=0) { nodes.addElement( original.substring(0, index) ); original = original.substring(index+separator.length()); index = original.indexOf(separator); } // Get the last node nodes.addElement( original ); // Create splitted string array String[] result = new String[ nodes.size() ]; if( nodes.size()>0 ) { for(int loop=0; loop<nodes.size(); loop++) { result[loop] = (String)nodes.elementAt(loop); //System.out.println(result[loop]); } } return result; } }
3 調(diào)用拆分函數(shù),實(shí)現(xiàn)字符串的拆分
int width = getWidth(); Font ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE); info = "歡迎使用!\n" +"1 MVC測(cè)試;\n" +"2 自動(dòng)換行測(cè)試,繪制可自動(dòng)識(shí)別換行的字符串。\n"; info_wrap = StringDealMethod.format(info, width-10, ft);
4 繪制字符串
graphics.setColor(Color.text); graphics.setFont(ft); for(int i=0; i<info_wrap.length; i++) { graphics.drawString(info_wrap[i], 5, i * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT); }
繪制的效果如圖1所示:
圖1 自動(dòng)換行字符串繪制效果
上述就是小編為大家分享的JavaMe開(kāi)發(fā)中怎么繪制可自動(dòng)換行文本了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:JavaMe開(kāi)發(fā)中怎么繪制可自動(dòng)換行文本
地址分享:http://chinadenli.net/article24/ppgjce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、虛擬主機(jī)、Google、標(biāo)簽優(yōu)化、商城網(wǎng)站、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)