手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

用J2ME在移动设备上实现动画的实例讲解

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

AnimatedImage类扩展了java.util.TimerTask,允许你设定一个timer。这里有个例子说明如何使用timer做动画:

Timer timer = new Timer();
AnimatedImage ai = ..... // get the image
timer.schedule( ai, 200, 200 );

每隔大约200毫秒,timer调用AnimatedImage.run()方法一次,这个方法使得动画翻滚到下一个帧。现在我们需要的是让MIDlet 来试试显示动画!我们定义一个简单的Canvas类的子类,好让我们把动画“粘贴上去”。

import java.util.*;
import javax.microedition.lcdui.*;
// A canvas to which you can attach one or more
// animated images. When the canvas is painted,
// it cycles through the animated images and asks
// them to paint their current image.
public class AnimatedCanvas extends Canvas {;
private Display display;
private Image offscreen;
private Vector images = new Vector();
public AnimatedCanvas( Display display ){;
this.display = display;
// If the canvas is not double buffered by the
// system, do it ourselves...
if( !isDoubleBuffered() ){;
offscreen = Image.createImage( getWidth(),
getHeight() );
};
};
// Add an animated image to the list.
public void add( AnimatedImage image ){;
images.addElement( image );
};
// Paint the canvas by erasing the screen and then
// painting each animated image in turn. Double
// buffering is used to reduce flicker.
protected void paint( Graphics g ){;
Graphics saved = g;
if( offscreen != null ){;
g = offscreen.getGraphics();
};
g.setColor( 255, 255, 255 );
g.fillRect( 0, 0, getWidth(), getHeight() );
int n = images.size();
for( int i = 0; i < n; i ){;
AnimatedImage img = (AnimatedImage)
images.elementAt( i );
img.draw( g );
};
if( g != saved ){;
saved.drawImage( offscreen, 0, 0,
Graphics.LEFT | Graphics.TOP );
};
};
};

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只小鸟,小鸟的位置和刷新速度是随机的。你可以用一些其他的办法来改进这个程序,但这个程序也应该足够能让你上手了。

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!