Canvas类提供了一个使用低级界面编程来处理图形的方法,可以使用getWidth()和getHeight()获取移动设备的屏幕宽度和长度.
下边的代码说明了如何在移动设备屏幕上画一个矩形:
public void paint(Graphics g)
{
// 获取屏幕的长和宽:
int width = getWidth ();
int height = getHeight();
// 将整个屏幕清空:
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
Font font = g.getFont();
int fontHeight = font.getHeight();
int fontWidth = font.stringWidth("Hello World!");
// 将文本颜色设为红色:
g.setColor(255, 0, 0);
g.setFont(font);
g.drawString("Hello World!", (width - fontWidth)/2,
(height - fontHeight)/2,g.TOP|g.LEFT);
}
本文介绍了一种在移动设备上使用Canvas类进行绘图的方法。通过getWidth()和getHeight()获取屏幕尺寸,并演示了如何绘制一个填充了背景色的矩形及居中显示的红色文本“HelloWorld!”。

被折叠的 条评论
为什么被折叠?



