|
欢迎界面一般可以通过以下两种方式,进入主画面。当用户按键,进入主画面、等待N秒自动进入主画面。等待n秒,这里也有两种方式:
1)让线程睡眠n秒,
2)定时器设定n秒,到n秒后去调度任务,进入主画面
其中我们主要用到的是触摸屏处理、按键处理、定时器 、线程。下面我们就来看看怎么才能用canvas来实现欢迎界面。
java代码:
package eoe.android;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class WelcomeCanvas extends Canvas {
private Display display;
private Displayable nextUI;
private Timer timer = new Timer();
private long displayTime = 3000;
public WelcomeCanvas(Display dis, Displayable disp) {
this.display = dis;
this.nextUI = disp;
}
protected void paint(Graphics arg0) {
int width = this.getWidth();
int height = this.getHeight();
Image displayImage = null;
try {
displayImage = Image.createImage(“/logo20k.png”);
} catch (IOException e) {
e.printStackTrace();
}
arg0.drawImage(displayImage, getWidth()/2, getHeight()/2,Graphics.HCENTER|Graphics.VCENTER);
}
public void setDisplayTime(long dispTime) {
this.displayTime = dispTime;
}
protected void keyPressed(int keyCode) {
cancelWelcome();
}
protected void pointerPressed(int y, int x) {
cancelWelcome();
}
private void cancelWelcome() {
timer.cancel();
display.setCurrent(nextUI);
}
protected void showNotify() {
timer.schedule(new TimerTask() {
public void run() {
cancelWelcome();
}
}, displayTime);
}
}
Thread方式实现自动进入主画面
Java代码:
package EOE.th;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class WelcomeCanvas2 extends Canvas implements Runnable{
private Display display;
private Displayable nextUI;
private long displayTime = 3000;
public WelcomeCanvas2(Display dis, Displayable disp) {
this.display = dis;
this.nextUI = disp;
//启动线程
Thread thread = new Thread(this);
thread.start();
}
protected void paint(Graphics arg0) {
int width = this.getWidth();
int height = this.getHeight();
Image displayImage = null;
try {
displayImage = Image.createImage(“/logo20k.png”);
} catch (IOException e) {
e.printStackTrace();
}
arg0.drawImage(displayImage, getWidth()/2, getHeight()/2, Graphics.HCENTER|Graphics.VCENTER);
}
public void run(){
//等待3秒
try{
Thread.sleep(displayTime);
}catch(Exception e){
}
//显示需要显示的界面
display.setCurrent(nextUI);
}
public void setDisplayTime(long dispTime) {
this.displayTime = dispTime;
}
protected void keyPressed(int keyCode) {
cancelWelcome();
}
protected void pointerPressed(int y, int x) {
cancelWelcome();
}
private void cancelWelcome() {
display.setCurrent(nextUI);
}
} |
|