| 用J2ME在移动设备上实现动画 |
|
|
|
|
Author: 一滴蔚蓝色 | Date: 2007-12-02 | View: 1004
|
开发技术 -
新手上路
| ||||
|
使用MIDP(Mobile Information Device Profile)的开发人员经常会抱怨用些什么办法才可以在一个MIDlet上显示动画。MIDP 1.0 没有直接提供对动画的支持(MIDP 2.0支持),但真要是自己去实现,其实也并非是一件很难的事。 首先要做的,是使用你的图片处理软件(比如ps或者firework)创建一系列相同大小的图片来组成动画。每张图片代表动画一帧。你需要制作一定数量的祯--越多的帧会让你的动画看上去越平滑。制作好的图片一定要保存成PNG(Portable Network Graphics)格式,MIDP唯一支持的图片格式。 有两个办法让你刚做好的图片在MIDlet上变成动画。第一,把图片都放到一个web服务器上,让MIDlet下载他们,MIDP内置的HTTP支持。第二个办法更简单,把图片用MIDlet打包成jar文件。如果你使用的是J2ME开发工具,把PNG文件放你的项目文件里面就可以了。 动画的过程其实更像帐本记录:显示当前帧,然后适当地更换到下一帧。那么使用一个类来完成这个工作应该是很恰当的,那好,我们就先定义一个AnimatedImage类: import java.util.*; // Construct an animation with no canvas. public AnimatedImage( Image[] images ){; // Construct an animation with a null clip list. public AnimatedImage( Canvas canvas, Image[] images ) // Construct an animation. The canvas can be null, public AnimatedImage( Canvas canvas, Image[] images, int[][] clipList ){; if( images != null && clipList != null ){; if( images != null && images.length > 0 ){; // Move to the next frame, wrapping if necessary. public void advance( boolean repaint ){; if( repaint && canvas != null && canvas.isShown() ){; // Draw the current image in the animation. If public void draw( Graphics g ){; int[] list = clipList[which]; for( int i = 0; i + 3 <= list.length; i +=4 ){; g.setClip( cx, cy, cw, ch ); // Moves the animation′s top left corner. public void move( int x, int y ){; // Invoked by the timer. Advances to the next frame public void run(){; 你实例化一个AnimatedImage对象的时候你必须给AnimatedImage类的构造方法传一个Image对象数组,该数组代表动画的每一帧。使用的所有图片必须具有相同的高度和宽度。 用Image.createImage()方法从jar文件里面加载图片: private Image[] loadFrames( String name, int frames ) 你也可以传递一个Canvas对象(可选),和一个剪辑列表(clip list)。如果你指定了一个canvas和使用一个timer来自动更换到动画的下一帧,就如下面的例子代码中一样,canvas在动画向前滚动以后自动被重画(repaint)。不过这样的实现办法是可选的,你可以这样做,也可以让程序选择合适的时候重画canvas。 本文关键字: J2ME 移动设备 动画 阅读数: 1005 | 打印 | E-mail
|
||||||||
| (使用J2ME高级用户界面(猜数字)) < 上一篇 | 下一篇 > (Java游戏编程读书笔记) |
|---|