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

本文介绍如何使用 Java 的 TimerTask 实现 Logo 的自动切换,包括定时任务的设置、图片显示的方法及利用 currentTimeMillis 实现延时的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请参考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.

ExpandedBlockStart.gifpublic void testTimer() {
InBlock.gifMyTimerTask myTimerTask 
= new MyTimerTask();
InBlock.gifTimer timer 
= new Timer();
InBlock.giftimer.schedule(myTimerTask, 
500010000); //wait for 5 seconds and then call the function every 
InBlock.gif

InBlock.gif
10 seconds
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gif
class MyTimerTask extends TimerTask {
ExpandedSubBlockStart.gif
public void run() {
InBlock.gif
//This method will be called every 10 Seconds
InBlock.gif

InBlock.gifImage im 
= Image.createImage(imageData, 0, imageData.length);
InBlock.gif
if(im == null)
InBlock.gifSystem.out.println(
"NULL IMAGE");
InBlock.gifSystem.out.println(
"The Size of the Byte Array is:" +imageData);
InBlock.gif
if(frm.size() > 0)
InBlock.gif
for(int i = 0; i < frm.size(); i++)
InBlock.giffrm.delete(i);
InBlock.giffrm.append(im);
InBlock.gifdisp.setCurrent(frm);
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

 

 

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

None.gifimport java.util.*;
None.gif
None.gif
import javax.microedition.lcdui.*;
None.gif
None.gif
public class WaitCanvas
ExpandedBlockStart.gif
extends Canvas {
InBlock.gif
private int mCount, mMaximum;
InBlock.gif
private int mInterval;
InBlock.gif
InBlock.gif
private int mWidth, mHeight, mX, mY, mRadius;
InBlock.gif
private String mMessage;
InBlock.gif
ExpandedSubBlockStart.gif
public WaitCanvas() {
InBlock.gifmCount 
= 0;
InBlock.gifmMaximum 
= 36;
InBlock.gifmInterval 
= 100;
InBlock.gif
InBlock.gifmWidth 
= getWidth();
InBlock.gifmHeight 
= getHeight();
InBlock.gif
InBlock.gif
// Calculate the radius.
InBlock.gif
int halfWidth = (mWidth - mRadius) / 2;
InBlock.gif
int halfHeight = (mHeight - mRadius) / 2;
InBlock.gifmRadius 
= Math.min(halfWidth, halfHeight);
InBlock.gif
InBlock.gif
// Calculate the location.
InBlock.gif
mX = halfWidth - mRadius / 2;
InBlock.gifmY 
= halfHeight - mRadius / 2;
InBlock.gif
InBlock.gif
// Create a Timer to update the display.
ExpandedSubBlockStart.gif
TimerTask task = new TimerTask() {
ExpandedSubBlockStart.gif
public void run() {
InBlock.gifmCount 
= (mCount + 1% mMaximum;
InBlock.gifrepaint();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
;
InBlock.gifTimer timer 
= new Timer();
InBlock.giftimer.schedule(task, 
0, mInterval);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
public void setMessage(String s) {
InBlock.gifmMessage 
= s;
InBlock.gifrepaint();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
public void paint(Graphics g) {
InBlock.gif
int theta = -(mCount * 180 / mMaximum);
InBlock.gif
InBlock.gif
InBlock.gif
// Clear the whole screen.
InBlock.gif
g.setColor(255255255);
InBlock.gifg.fillRect(
00, mWidth, mHeight);
InBlock.gif
InBlock.gif
// Now draw the pinwheel.
InBlock.gif
g.setColor(000);
InBlock.gif
InBlock.gifg.drawArc(mX, mY, mRadius, mRadius, 
0360);
InBlock.gif
InBlock.gifg.fillArc(mX, mY, mRadius, mRadius, theta 
+ 2020);
InBlock.gif
//g.fillArc(mX, mY, mRadius, mRadius, theta + 60, 60);
InBlock.gif
//g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90);
InBlock.gif
//g.fillArc(mX, mY, mRadius, mRadius, theta + 120, 120);
InBlock.gif
InBlock.gif
// Draw the message, if there is a message.
InBlock.gif
if (mMessage != null)
InBlock.gifg.drawString(mMessage, mWidth 
/ 2, mHeight,
InBlock.gifGraphics.BOTTOM 
| Graphics.HCENTER);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif


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


 

None.gifimport java.util.*;
None.gif
import javax.microedition.lcdui.*;
None.gif
ExpandedBlockStart.gif
public class Splash extends Canvas {
InBlock.gif
InBlock.gif
private Display display;
InBlock.gif
private Displayable next;
InBlock.gif
private Timer timer=new Timer();
InBlock.gif
ExpandedSubBlockStart.gif
public Splash (Display display,Displayable next) {
InBlock.gif
this.display=display;
InBlock.gif
this.next=next;
InBlock.gifdisplay.setCurrent(
this);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
protected void showNotify () {
ExpandedSubBlockStart.giftimer.schedule( 
new TimerTask () public void run() {
ExpandedSubBlockEnd.gifdisplayNext(); }
},8000);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
protected void hideNotify() {
InBlock.giftimer.cancel();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
protected void keyPressed (int keycode) {
InBlock.gifdisplayNext();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
protected void pointerPressed (int x, int y) {
InBlock.gifdisplayNext();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
private void displayNext() {
InBlock.gifdisplay.setCurrent(next);
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gif
protected void paint (Graphics g) {
InBlock.gif
int height=this.getHeight();
InBlock.gif
int width=this.getWidth();
InBlock.gif
InBlock.gif
// fill background as white
InBlock.gif
g.setColor(0xFFFFFF);
InBlock.gifg.fillRect(
0,0,width,height);
InBlock.gif
InBlock.gifImage logo
=null;
ExpandedSubBlockStart.gif
try {
InBlock.giflogo
=Image.createImage("/images/logo.png");
ExpandedSubBlockStart.gif}
 catch (Exception ignore) {}
InBlock.gif
InBlock.gifg.drawImage(logo,width
/2,height/2,g.HCENTER|g.VCENTER);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gifhere
's the calling method in your midlet(it passes the Display and current Displayable):
None.gif

ExpandedBlockStart.gif
/**
InBlock.gif* This shows the splash
ExpandedBlockEnd.gif
*/
None.gif
ExpandedBlockStart.gif
private void showSplash () {
InBlock.gif
new Splash (display,MenuList); 
ExpandedBlockEnd.gif}

None.gif
None.gif



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

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

 

None.gifprivate boolean showImage;
None.gifdot.gif
None.gif
None.gif
void someMethod()
ExpandedBlockStart.gif
{
InBlock.gif
long time = System.currentTimeMillis()+2000;
InBlock.gif
InBlock.gifshowImage 
= true;
InBlock.gif
while(System.currentTimeMillis()<time)
ExpandedSubBlockStart.gif
{
InBlock.gifrepaint();
InBlock.gifserviceRepaints();
ExpandedSubBlockEnd.gif}

InBlock.gifshowImage 
= false;
ExpandedBlockEnd.gif}

None.gif
None.gif
public void paint()
ExpandedBlockStart.gif
{
InBlock.gifdot.gif
InBlock.gif
if(showImage)
InBlock.gifg.drawImage(img,offsetX,MAX_Y
/2,g.LEFT|g.VCENTER);
ExpandedBlockEnd.gif}

None.gif

 

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值