[JavaME]利用java.util.TimerTask来做Splash Screen的N种方法

简介:

请参考java.util.TimerTask.

TimerTask is something like Timer in VisualBasic. You can sepcify a time period in milliseconds

for your requirement"一幅LOGO显示完以后,几秒种自动显示下一幅LOGO". 
Here is an sample code.

public   void  testTimer()  {
MyTimerTask myTimerTask 
= new MyTimerTask();
Timer timer 
= new Timer();
timer.schedule(myTimerTask, 
500010000); //wait for 5 seconds and then call the function every 

10 seconds
}


class  MyTimerTask  extends  TimerTask  {
public void run() {
//This method will be called every 10 Seconds

Image im 
= Image.createImage(imageData, 0, imageData.length);
if(im == null)
System.out.println(
"NULL IMAGE");
System.out.println(
"The Size of the Byte Array is:" +imageData);
if(frm.size() > 0)
for(int i = 0; i < frm.size(); i++)
frm.delete(i);
frm.append(im);
disp.setCurrent(frm);

}

}

 

 

另外,对于你所说的是不是应该叫做SplashScreen,那么国外曾经有人给出这么一个例子,虽然不是周期性地显示一张又一张的图片,而是利用TimerTask周期性地repaint画布,画出一种Splash Screen的感觉,你可以参考:

import  java.util. * ;

import  javax.microedition.lcdui. * ;

public   class  WaitCanvas
extends  Canvas  {
private int mCount, mMaximum;
private int mInterval;

private int mWidth, mHeight, mX, mY, mRadius;
private String mMessage;

public WaitCanvas() {
mCount 
= 0;
mMaximum 
= 36;
mInterval 
= 100;

mWidth 
= getWidth();
mHeight 
= getHeight();

// Calculate the radius.
int halfWidth = (mWidth - mRadius) / 2;
int halfHeight = (mHeight - mRadius) / 2;
mRadius 
= Math.min(halfWidth, halfHeight);

// Calculate the location.
mX = halfWidth - mRadius / 2;
mY 
= halfHeight - mRadius / 2;

// Create a Timer to update the display.
TimerTask task = new TimerTask() {
public void run() {
mCount 
= (mCount + 1% mMaximum;
repaint();
}

}
;
Timer timer 
= new Timer();
timer.schedule(task, 
0, mInterval);
}


public void setMessage(String s) {
mMessage 
= s;
repaint();
}


public void paint(Graphics g) {
int theta = -(mCount * 180 / mMaximum);


// Clear the whole screen.
g.setColor(255255255);
g.fillRect(
00, mWidth, mHeight);

// Now draw the pinwheel.
g.setColor(000);

g.drawArc(mX, mY, mRadius, mRadius, 
0360);

g.fillArc(mX, mY, mRadius, mRadius, theta 
+ 2020);
//g.fillArc(mX, mY, mRadius, mRadius, theta + 60, 60);
//g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90);
//g.fillArc(mX, mY, mRadius, mRadius, theta + 120, 120);

// Draw the message, if there is a message.
if (mMessage != null)
g.drawString(mMessage, mWidth 
/ 2, mHeight,
Graphics.BOTTOM 
| Graphics.HCENTER);
}

}



上面那个是利用TimerTask自动定时填充图形来展示Splash Screen的,那么下面这个就是显示图片来Splash Screen了:


 

import  java.util. * ;
import  javax.microedition.lcdui. * ;

public   class  Splash  extends  Canvas  {

private Display display;
private Displayable next;
private Timer timer=new Timer();

public Splash (Display display,Displayable next) {
this.display=display;
this.next=next;
display.setCurrent(
this);
}


protected void showNotify () {
timer.schedule( 
new TimerTask () public void run() {
displayNext(); }
}
,8000);
}


protected void hideNotify() {
timer.cancel();
}


protected void keyPressed (int keycode) {
displayNext();
}


protected void pointerPressed (int x, int y) {
displayNext();
}


private void displayNext() {
display.setCurrent(next);
}
 

protected void paint (Graphics g) {
int height=this.getHeight();
int width=this.getWidth();

// fill background as white
g.setColor(0xFFFFFF);
g.fillRect(
0,0,width,height);

Image logo
=null;
try {
logo
=Image.createImage("/images/logo.png");
}
 catch (Exception ignore) {}

g.drawImage(logo,width
/2,height/2,g.HCENTER|g.VCENTER);
}


}
 

here
' s the calling method in your midlet(it passes the Display and current Displayable):

/**
* This shows the splash
*/


private   void  showSplash ()  {
new Splash (display,MenuList); 
}




还有一种办法是利用currentTimeMillis。
无非就是利用System.currentTimeMillis()+2000先行计算出什么时间该显示

后一幅图片了,如果靠while循环不断检测发现时间到了,就换那张图片。

 

private   boolean  showImage;


void  someMethod()
{
long time = System.currentTimeMillis()+2000;

showImage 
= true;
while(System.currentTimeMillis()<time)
{
repaint();
serviceRepaints();
}

showImage 
= false;
}


public   void  paint()
{

if(showImage)
g.drawImage(img,offsetX,MAX_Y
/2,g.LEFT|g.VCENTER);
}

 

efei说:
“你要做的无非就是一个延时,过一定时间就换一幅图片。至于怎么来判断这个延时,方法多种多样,用线程,用TimerTask,用System.currentTimeMillis(),基本上都一样

个人比较倾向于使用线程作为固定的时钟脉冲来驱动游戏。

对于System.currentTimeMillis(),我只能告诉你两点,一是它的作用是取得当前时间,二呢,用这个方法如果只是简单比较时间,那么如果中断游戏,过一段时间再恢复,就会存在问题。

目录
相关文章
|
10天前
|
算法 Java Linux
java制作海报二:java使用Graphics2D 在图片上合成另一个照片,并将照片切割成头像,头像切割成圆形方法详解
这篇文章介绍了如何使用Java的Graphics2D类在图片上合成另一个照片,并将照片切割成圆形头像的方法。
22 1
java制作海报二:java使用Graphics2D 在图片上合成另一个照片,并将照片切割成头像,头像切割成圆形方法详解
|
4天前
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
16 1
|
8天前
|
Java 编译器
Java“返回类型为 void 的方法不能返回一个值”解决
在 Java 中,如果一个方法的返回类型被声明为 void,那么该方法不应该包含返回值的语句。如果尝试从这样的方法中返回一个值,编译器将报错。解决办法是移除返回值语句或更改方法的返回类型。
|
1月前
|
Java
Java——方法的引用
方法引用允许将已有方法作为函数式接口的实现。使用“::”符号,需具备函数式接口,被引用的方法须存在且参数和返回值需与抽象方法一致。其分类包括:静态方法引用(类::方法名)、成员方法引用(对象::方法名、this::方法名、super::方法名)和构造方法引用(类名::new)。方法引用提高了代码的简洁性和可读性,减少了样板代码。
37 13
Java——方法的引用
|
8天前
|
Java
让星星⭐月亮告诉你,Java NIO之Buffer详解 属性capacity/position/limit/mark 方法put(X)/get()/flip()/compact()/clear()
这段代码演示了Java NIO中`ByteBuffer`的基本操作,包括分配、写入、翻转、读取、压缩和清空缓冲区。通过示例展示了`position`、`limit`和`mark`属性的变化过程,帮助理解缓冲区的工作原理。
17 2
|
8天前
|
Java
让星星⭐月亮告诉你,jdk1.8 Java函数式编程示例:Lambda函数/方法引用/4种内建函数式接口(功能性-/消费型/供给型/断言型)
本示例展示了Java中函数式接口的使用,包括自定义和内置的函数式接口。通过方法引用,实现对字符串操作如转换大写、数值转换等,并演示了Function、Consumer、Supplier及Predicate四种主要内置函数式接口的应用。
14 1
|
8天前
|
Java
让星星⭐月亮告诉你,Java synchronized(*.class) synchronized 方法 synchronized(this)分析
本文通过Java代码示例,介绍了`synchronized`关键字在类和实例方法上的使用。总结了三种情况:1) 类级别的锁,多个实例对象在同一时刻只能有一个获取锁;2) 实例方法级别的锁,多个实例对象可以同时执行;3) 同一实例对象的多个线程,同一时刻只能有一个线程执行同步方法。
9 1
|
13天前
|
Java 编译器
在Java中,关于final、static关键字与方法的重写和继承【易错点】
在Java中,关于final、static关键字与方法的重写和继承【易错点】
19 5
|
1月前
|
Java
java基础(12)抽象类以及抽象方法abstract以及ArrayList对象使用
本文介绍了Java中抽象类和抽象方法的使用,以及ArrayList的基本操作,包括添加、获取、删除元素和判断列表是否为空。
21 2
java基础(12)抽象类以及抽象方法abstract以及ArrayList对象使用
|
10天前
|
存储 算法 Java
java制作海报六:Graphics2D的RenderingHints方法参数详解,包括解决文字不清晰,抗锯齿问题
这篇文章是关于如何在Java中使用Graphics2D的RenderingHints方法来提高海报制作的图像质量和文字清晰度,包括抗锯齿和解决文字不清晰问题的技术详解。
17 0
java制作海报六:Graphics2D的RenderingHints方法参数详解,包括解决文字不清晰,抗锯齿问题