选择显示字体大小

用j2me在移动设备上实现动画(1)

使用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.*;

import javax.microedition.lcdui.*;

// 定义了一个动画,该动画其实只是一系列相同大小的图片

// 轮流显示,然后模拟出的动画

public class animatedimage extends timertask {;

private canvas canvas;

private image[] images;

private int[][] cliplist;

private int current;

private int x;

private int y;

private int w;

private int h;

// construct an animation with no canvas.

public animatedimage( image[] images ){;

this( null, images, null );

};

// construct an animation with a null clip list.

public animatedimage( canvas canvas, image[]

images ){; this( canvas, images, null );

};

// construct an animation. the canvas can be null,

// but if not null then a repaint will be triggered

// on it each time the image changes due to a timer

// event. if a clip list is specified, the image is

// drawn multiple times, each time with a different

// clip rectangle, to simulate transparent parts.

public animatedimage( canvas canvas, image[] images,

int[][] cliplist ){;

this.canvas = canvas;

this.images = images;

this.cliplist = cliplist;

if( images != null && cliplist != null ){;

if( cliplist.length < images.length ){;

throw new illegalargumentexception();

};

};

if( images != null && images.length > 0 ){;

w = images[0].getwidth();

h = images[0].getheight();

};

};

// move to the next frame, wrapping if necessary.

public void advance( boolean repaint ){;

if( ++current >= images.length ){;

current = 0;

};

if( repaint && canvas != null && canvas.isshown()

){;

canvas.repaint( x, y, w, h );

canvas.servicerepaints();

};

};

// draw the current image in the animation. if

// no clip list, just a simple copy, otherwise

// set the clipping rectangle accordingly and

// draw the image multiple times.

public void draw( graphics g ){;

if( w == 0 h == 0 ) return;

int which = current;

if( cliplist == null cliplist[which] == null

){;

g.drawimage( images[which], x, y,

g.top g.left );

}; else {;

int cx = g.getclipx();

int cy = g.getclipy();

int cw = g.getclipwidth();

int ch = g.getclipheight();

int[] list = cliplist[which];

for( int i = 0; i + 3 <= list.length; i +=

4 ){;

g.setclip( x + list[0], y + list[1],

list[2], list[3] );

g.drawimage( images[which], x, y,

g.top g.left );

};

g.setclip( cx, cy, cw, ch );

};

};

// moves the animation's top left corner.

public void move( int x, int y ){;

this.x = x;

this.y = y;

};

// invoked by the timer. advances to the next frame

// and causes a repaint if a canvas is specified.

public void run(){;

if( w == 0 h == 0 ) return;

advance( true );

};

};

你实例化一个animatedimage对象的时候你必须给animatedimage类的构造方法传一个image对象数组,该数组代表动画的每一帧。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons