?? splashcanvas.java
字號:
package com.j2medev.chapter3.splash;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.*;
public class SplashCanvas extends Canvas {
private Welcome midlet = null;//保存MIDlet的引用方便回調
private Displayable nextUI = null;
private Timer timer = new Timer();//使用Timer執行循環繪圖的功能
private int width;
private int height;
private int y = 0;//y代表變化的clipY值
//第一次執行paint()的時候需要執行clearBackground()
private boolean first = true;
private Image img = null;
private Font font = null;
public SplashCanvas(Welcome midlet, Displayable disp) {
this.midlet = midlet;
this.nextUI = disp;
width = this.getWidth();
height = this.getHeight();
font = Font.getDefaultFont();
//初始化圖片
try {
img = Image.createImage("/logo.png");
} catch (IOException e) {
e.printStackTrace();
}
//初始化y值
y=height/2-img.getHeight()/2;
}
protected void paint(Graphics g) {
if(first){
clearBackground(g);
first = false;
}
//設置裁減區域
g.setClip(0,y, width, 5);
//繪制圖片
g.drawImage(img, width / 2, height/ 2, Graphics.HCENTER| Graphics.VCENTER);
//圖片繪制完成后就繪畫文本
if(y> (height+img.getHeight())/2){
g.drawString("與朋友分享圖片", width/2, (height+img.getHeight())/2+font.getHeight(), Graphics.HCENTER|Graphics.TOP);
}
}
private void clearBackground(Graphics g){
//清除背景
int color = g.getColor();
g.setColor(0xFFFFFF);
g.fillRect(0,0, width,height);
g.setColor(color);
}
//任意鍵被按下就跳轉到下個UI
protected void keyPressed(int keyCode) {
dismiss();
}
protected void pointerPressed(int y, int x) {
dismiss();
}
//停止timer并進入下個UI
private void dismiss() {
timer.cancel();
midlet.setCurrent(nextUI);
}
//在Canvas被顯示的時候開始執行TimerTask的內容
protected void showNotify() {
timer.schedule(new TimerTask() {
public void run() {
repaint();
//畫完圖片和文字后就進入下個UI
if(y > (height + img.getHeight())/ 2+3*font.getHeight()){
//休眠500ms
try{
Thread.sleep(500);
}catch(Exception ex){
ex.printStackTrace();
}
dismiss();
}
y = y + 5;
}
},0,150);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -