这个函数已反复应用于多个手机应用软件平台
用法:参数定义:str――要分割的字符串
font――字体
rowmaxw――分割后每行宽度
支持标示符:
\n 换行
\t 插入两个汉字长度的空格
| public static final string[] clipstring(string str,font font,int rowmaxw){ if(str == null) return null; if(rowmaxw < font.charwidth('哈')) rowmaxw = font.charwidth('哈'); int strid = 0; int roww = 0; vector strmanager = new vector(); char ch = ' '; while(str.length() > strid){ ch = str.charat(strid); switch(ch) { case '\n': strmanager.addelement(str.substring(0,strid)); str = str.substring(strid+1); roww = 0; strid = 0; break; case '\t': stringbuffer sb = new stringbuffer(str); sb.deletecharat(strid); sb.insert(strid," "); str = sb.tostring(); break; default: if(roww + font.charwidth(ch) > rowmaxw){ strmanager.addelement(str.substring(0,strid)); str = str.substring(strid); roww = 0; strid = 0; }else{ roww += font.charwidth(ch); strid++; } } } strmanager.addelement(str); string[] o_str = new string[strmanager.size()]; strmanager.copyinto(o_str); return o_str; } |
返回结果是一个已切割好的string数组,只要用一个循环打印出来就可以了
| public static final void drawclipstring(graphics g,string[] clipstr,font font,int color,int x,int y){ if(clipstr == null){ system.out.println("drawclipstring"); return; } int fonth = font.getheight(); g.setfont(font); g.setcolor(color); for(int i=0;i<clipstr.length;i++) g.drawstring(clipstr[ i ],x,y+i*fonth,0); } |
参数定义:clipstr――先前分割好的字符串数组
font――字体
color――颜色
x,y――打印的屏幕位置
注意,切割和打印函数的字体参数必须保持一致!
半透明技术(限midp2.0)
| // 获得半透明图片,透明度从0到10共分为11个等级 public static final image alfimage(image img,int alf){ if(img == null){ system.out.println("alfimage"); return null; } if(alf < 0) alf = 0; else if(alf > 10) alf = 10; int imgw = img.getwidth(); int imgh = img.getheight(); int[] rgbdata = new int[imgw*imgh]; img.getrgb(rgbdata,0,imgw,0,0,imgw,imgh); int tmp = ((alf*255/10) << 24)|0x00ffffff; for(int i=0;i<rgbdata.length;i++) rgbdata &= tmp; image o_img = image.creatergbimage(rgbdata,imgw,imgh,true); return o_img; } |
灰度图转化函数
|
// 得到灰度图 |
闽公网安备 35060202000074号