科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道用J2ME在移动设备上实现动画

用J2ME在移动设备上实现动画

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

使用MIDP(Mobile Information Device Profile)的开发人员经常会抱怨用些什么办法才可以在一个MIDlet上显示动画。 MIDP 1.0 没有直接提供对动画的支持(正在开发中的MIDP 2.0支持),但真要是自己去实现,其实也并非是一件很难的事……

作者:51CTO.com整理 来源:51CTO.com 2007年9月4日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共3页)

AnimatedCanvas 类的代码相当简单,由一个动画导入方法和一个paint方法。canvas画布每次被画,背景都会被擦除,然后循环每个导入的AnimatedImage对象,直接画到自己身上来(自己扩展了canvas类)。

import java.io.*;

import java.util.*;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

// MIDlet that displays some simple animations.

// Displays a series of birds on the screen and

// animates them at different (random) rates.

public class AnimationTest extends MIDlet

implements CommandListener {;

private static final int BIRD_FRAMES = 7;

private static final int NUM_BIRDS = 5;

private Display display;

private Timer timer = new Timer();

private AnimatedImage[] birds;

private Random random = new Random();

public static final Command exitCommand =

new Command( "Exit",

Command.EXIT, 1 );

public AnimationTest(){;

};

public void commandAction( Command c,

Displayable d ){;

if( c == exitCommand ){;

exitMIDlet();

};

};

protected void destroyApp( boolean unconditional )

throws MIDletStateChangeException {;

exitMIDlet();

};

public void exitMIDlet(){;

timer.cancel(); // turn it off...

notifyDestroyed();

};

// Generate a non-negative random number...

private int genRandom( int upper ){;

return( Math.abs( random.nextInt() ) % upper );

};

public Display getDisplay(){; return display; };

// Initialize things by creating the canvas and then

// creating a series of birds that are moved to

// random locations on the canvas and attached to

// a timer for scheduling.

protected void initMIDlet(){;

try {;

AnimatedCanvas c = new

AnimatedCanvas( getDisplay() );

Image[] images =

loadFrames( "/images/bird",

BIRD_FRAMES );

int w = c.getWidth();

int h = c.getHeight();

birds = new AnimatedImage[ NUM_BIRDS ];

for( int i = 0; i < NUM_BIRDS; ++i ){;

AnimatedImage b = new

AnimatedImage( c, images );

birds = b;

b.move( genRandom( w ), genRandom( h ) );

c.add( b );

timer.schedule( b, genRandom( 1000 ),

genRandom( 400 ) );

};

c.addCommand( exitCommand );

c.setCommandListener( this );

getDisplay().setCurrent( c );

};

catch( IOException e ){;

System.out.println( "Could not

load images" );

exitMIDlet();

};

};

// Load the bird animation, which is stored as a

// series of PNG files in the MIDlet suite.

private Image[] loadFrames( String name, int frames )

throws IOException {;

Image[] images = new Image[frames];

for( int i = 0; i < frames; ++i ){;

images = Image.createImage( name +

i + ".png" );

};

return images;

};

protected void pauseApp(){;

};

protected void startApp()

throws MIDletStateChangeException {;

if( display == null ){;

display = Display.getDisplay( this );

initMIDlet();

};

};

};

七帧图片的动画,你可以看到一个拍着翅膀的小鸟。MIDlet显示了5只小鸟,小鸟的位置和刷新速度是随机的。你可以用一些其他的办法来改进这个程序,但这个程序也应该足够能让你上手了。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章