在移动设备上用J2ME实现动画
http://tech.ddvip.com 2006年04月01日 社区交流
本文详细介绍在移动设备上用J2ME实现动画
每隔大约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 );
};
};
};
责编:豆豆技术应用