WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发用J2ME在移动设备上实现动画

用J2ME在移动设备上实现动画
作者:佚名  来源:转载  发布时间:2005-8-10 11:30:55
一样,canvas在动画向前滚动以后自动被重画(repaint)。不过这样的实现办法是可选的,
你可以这样做,也可以让程序选择合适的时候重画canvas。

因为MIDP 1.0不支持透明的图片,AnimatedImage 类使用一个剪辑列表来模拟透明的效果,剪辑列表是图片被剪成的方块
区域的系列。图片被画出来的时候分开几次,每次画一个剪辑列表里面的剪辑区域。剪辑列表在帧的基础上被定义好,所以
你需要为图片的每一帧创建一个数组。数组的大小应该是4的倍数,因为每一个剪辑面积保持了四个数值:左坐标,顶坐标,
宽度以及高度。坐标的原点是整个图片的左上角。需要注意的是使用了剪辑列表会使动画慢下来。如果图片更加复杂的话,你应该
使用矢量图片。

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 =

上一页  [1] [2] [3]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码