
|
When working with system fonts on Sony Ericsson MIDP 2.0 mobile phones there are a few attributes in the font class that can be used to specify the font layout. Download MIDlet and source code here>>
The layout of the font is specified when creating the font. Note that currently on Sony Ericsson phones only the font face system is implemented. Specifying any of the other faces will still result in the system font face being used.
In addition to the standard font attributes, font effects can be created when using the system font on a Canvas or in a CustomItem. The effects that are shown in this example are:
The effect is actually done on an image which the font is painted on. graphics.drawString("my text", 0, 0, 0); And of course other effects can be made on the font image like transparency and resizing but this will not be included in this example.To create the image with the text we can do something like this: Image fontImage = Image.createImage(width, height); This will result in some black text drawn on a white background.
To change the look of this picture all pixels can be manipulated. For example, we can make all the white pixels transparent to mask out the background. int []data = new int[fontImage.getWidth() * fontImage.getHeight()]; for(int i=0; i<data.length; i++){ The color of every pixel in the image is stored in the format ARGB (Alpha, Red, Green, Blue) where every color is 1 byte with the value 0-255. To make the pixel transparent the Alpha value is set to zero.
To add a nice color effect to the text and make the color fade from a selected color to black, we just loop thorough the pixels and change the color of all pixels with the text color. Here all pixels that are not the same color as the background are changed. int []data = new int[fontImage.getWidth() * fontImage.getHeight()]; int offsetR = r/fontImage.getHeight(); newColor = 0xFF000000; // make the pixel opaque.
It is also quite easy to make a mono spaced font where each character uses the same width. int charWidth = font.charWidth('W'); graphics = fontImage.getGraphics(); for(int i=0; i<chars.length; i++){
|
Copyright © 2001 - 2009 Sony Ericsson Mobile Communications AB. All Rights Reserved.