📄 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 + -