站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题索爱MIDP 2.0手机的系统字体特效制作

索爱MIDP 2.0手机的系统字体特效制作

摘要: 索爱MIDP 2.0手机的系统字体特效制作
http://www.j2medev.com/Article/Class1/Class12/200612/4036.html

当你在索尼爱立信MIDP 2.0手机开发应用程序的时候,你可以使用一些系统字体的属性来帮助你设置字体的外观和布局。本文就是为大家介绍一下如何制作字体效果,包括渐变效果等。首先看看下图展示的字体效果。



字体的外形是在创建Font类的时候指定的,请注意目前SonyEricsson的手机只实现了FACE_SYSTEM类型的字体,如果你创建的时候指定其他外观也会自动使用FACE_SYSTEM。

Font font = Font.createFont(FONT.FACE_SYSTEM, FONT.SIZE_MEDIUM, FONT.STYLE_PLAIN);

FACE_SYSTEM
SIZE_LARGE
SIZE_MEDIUM
SIZE_SMALL
STYLE_BOLD
STYLE_ITALIC
STYLE_PLAIN
STYLE_UNDERLINED
可以在Canvas和CustomItem的界面中使用Font来装饰界面,下面列举几个例子。

下面的代码可以在Image上绘制文字,

Image fontImage = Image.createImage(width, height);
graphics = fontImage.getGraphics();
graphics.setFont(font);
graphics.setColor(0x00);
graphics.drawString("Hello World!", 0, 0, 0);

这样在白色背景后面绘制了黑色的文字,如下:



当然,我们可以把图片中白色的背景修改为透明的,这样如果把Image画到Canvas上的时候就不会显得很突兀了(当Canvas的颜色和背景不一致的时候)。实现这个并不困难,你只需要修改Image的每个象素的Alpha值,修改为0即可。

int []data = new int[fontImage.getWidth() * fontImage.getHeight()];
fontImage.getRGB(data, 0, fontImage.getWidth(), 0, 0, fontImage.getWidth(), fontImage.getHeight());

for(int i=0; iif(data[i]==backgroundColor){
data[i] = (data[i]&0x00FFFFFF);
}
}



同样的道理,我们不只可以改变透明度,还可以改变图片象素的颜色,这样就可以实现字颜色的渐变了。下面的代码可以实现颜色的改变,我们要做的就是循环改变图片的像素值,请注意每一行使用一个颜色。


int []data = new int[fontImage.getWidth() * fontImage.getHeight()];
fontImage.getRGB(data, 0, fontImage.getWidth(), 0, 0, fontImage.getWidth(), fontImage.getHeight());
int newColor = fontColor;
int r = (fontColor & 0x00FF0000)>>16;
int g = (fontColor & 0x0000FF00)>>8;
int b = (fontColor & 0x000000FF);

int offsetR = r/fontImage.getHeight();
int offsetG = g/fontImage.getHeight();
int offsetB = b/fontImage.getHeight();
for(int i=0; iif(data[i] != backgroundColor){
data[i] = newColor;
}
if(i%imageWidth == 0){ // subtract each color on every row.
if(r>offsetR)
r -= offsetR;
if(g>offsetG)
g -= offsetG;
if(b>offsetB)
b -= offsetB;

newColor = 0xFF000000; // make the pixel opaque.
newColor += (r<<16);
newColor += (g<<8);
newColor += b;
}
}

如果想绘制等间距的文字,那么使用指定的宽度一个字符一个字符的画,如下。

int charWidth = font.charWidth('W');
textWidth = charWidth * text.length();
fontImage = Image.createImage(width, weight);

graphics = fontImage.getGraphics();
graphics.setFont(font);
graphics.setColor(0x00);

for(int i=0; igraphics.drawChar(text.charAt(i),i*charWidth + charWidth/2, 0, Graphics.TOP | Graphics.HCENTER);
}






↑返回目录
前一篇: J2ME学习笔记(1)
后一篇: J2ME游戏开发中使用层的概念