1 基於MIDP的UI設計
上面的圖是整個MIDP中的LCDUI包的組成結構,這個包包括完整的MIDLP高級和低級UI庫。
1.1MIDP高級UI
我們在這一節要介紹LCDUI庫的具體組成類圖。Screen類系屬於高級圖形用戶界面組件,Canvas是低級圖形用戶界面組件,在同一時刻,只能有唯一一個Screen或者Canvas類的子類顯示在屏幕上。使用高級界面,但是九宮格這樣的屏幕鋪放應用的實現是想不到好的辦法,本身沒有合適的控件可以完成這個任務。一種可能的辦法是通過定製高級UI組件裏面的元素,比如CustomItem, 用他來繪製定宮格。本例就是使用CustomItem 來實現了一個九宮格效果,效果如下圖所示
源代碼如下所示:
public class GridItem extends CustomItem {
private Image image;
public MyItem(Image image, String title,int width,int height)
{
this.image = image;
}
public void paint(Graphics g, int w, int h)
{
g.drawImage(this.image, w/2, 0, Graphics.HCENTER | Graphics.TOP);
g.drawString(this.title, w/2, h - g.getFont().getHeight(), Graphics.HCENTER | Graphics.TOP);
}
通過對Form的定製item設置背景圖片,找到一個work around的辦法,放置我們的九宮格圖標,剩下的就是對Form主程序的設置了
Form f = new Form("CustomItem");
int tempWidth = f.getWidth() / 4;
int tempHeight = 60;
for(int i = 0; i < 9;i++)
{
String imgSrc = "/img/" + i + ".png";
try
{
Image img = Image.createImage(imgSrc);
MyItem mi = new MyItem(img,String.valueOf(i) + " item",tempWidth,tempHeight);
mi.setLayout( Item.LAYOUT_CENTER );
f.append( mi );
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
display.setCurrent( f );