How to use freely resizable font&nb
来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 09:12 点击:次
Contents [hide]
1 Overview
2 Source code: FontSizingMIDlet.java
3 Source code: FontCanvas.java
4 Example application
5 See also
1 Overview
2 Source code: FontSizingMIDlet.java
3 Source code: FontCanvas.java
4 Example application
5 See also
[edit]
Overview
Overview
In MIDlets the font and its properties are specified by using the standard LCDUI javax.microedition.lcdui.Font class. A font has three properties: size, style and face. In MIDP these properties have the following values:
Font size
SIZE_SMALL
SIZE_MEDIUM
SIZE_LARGE
Font size
SIZE_SMALL
SIZE_MEDIUM
SIZE_LARGE
Font style
STYLE_PLAIN
STYLE_BOLD
STYLE_ITALIC
STYLE_UNDERLINED
or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED
Font face
FACE_SYSTEM
FACE_MONOSPACE
FACE_PROPORTIONAL
Having only three sizes has been especially a limitation and this has been improved in the latest Java Runtimes for S60. It is now possible to use a new getFont() method in com.nokia.mid.ui.DirectUtils class:
public static Font getFont(int face, int style, int height)
face - one of FACE_SYSTEM, FACE_MONOSPACE, or FACE_PROPORTIONAL
style - STYLE_PLAIN, or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED
height - font height in pixels
This improvement is part of Nokia UI API 1.2, which is included in Java Runtime 1.3 for S60. Here is a full working sample MIDlet code below. The FontSizingMIDlet reads shows sample text on the screen and the font size and style can be changed by using the buttons on the touch screen. Because touch screen is used, this MIDlet works only in devices having one(for example, Nokia 5800 XpressMusic and N97).
[edit]
Source code: FontSizingMIDlet.java
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
public class FontSizingMIDlet extends MIDlet {
private FontCanvas canvas;
public void startApp() {
canvas = new FontCanvas(this);
Display.getDisplay(this).setCurrent(canvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
protected void showError(String title, String text) {
Alert alert = new Alert(title, text, null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
alert.getType().playSound(Display.getDisplay(this));
Displayable current = Display.getDisplay(this).getCurrent();
if (current instanceof Alert) {}
else Display.getDisplay(this).setCurrent(alert);
}
}
[edit]
Source code: FontSizingMIDlet.java
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
public class FontSizingMIDlet extends MIDlet {
private FontCanvas canvas;
public void startApp() {
canvas = new FontCanvas(this);
Display.getDisplay(this).setCurrent(canvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
protected void showError(String title, String text) {
Alert alert = new Alert(title, text, null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
alert.getType().playSound(Display.getDisplay(this));
Displayable current = Display.getDisplay(this).getCurrent();
if (current instanceof Alert) {}
else Display.getDisplay(this).setCurrent(alert);
}
}
[edit]
Source code: FontCanvas.java
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import com.nokia.mid.ui.DirectUtils;
import com.nokia.mid.ui.TactileFeedback;
public class FontCanvas extends Canvas implements CommandListener {
private FontSizingMIDlet midlet;
private Command exitCommand;
private String heightString = "";
private String fontString = "";
private Font font;
private int fontHeight = 20;
- 发表评论
-
- 最新评论 更多>>