Implement animation on mobile devices with J2ME

xiaoxiao2021-03-06  117

Developers using MIDP (Mobile Information Device Profile often complain what way can be used to display animations on a MIDLET. MIDP 1.0 did not directly provide support for animation (MIDP 2.0 support), but it is not a difficult thing to do.

The most basic prerequisite for any animation is to display and replace a picture in a fast enough time, let the eyes see the moving picture. The picture must be painted in order. From a picture to the next picture, the smaller the effect, the better the effect. The first thing to do is to create a series of pictures of the same size using your image processing software (such as PS or Firework) to form an animation. Each picture represents an animation frame. You need to make a certain number of persons - more frames will make your animation look smoothed. Making pictures must be saved into a PNG (Portable Network Graphics) format, MIDP uniquely supported picture format. There are two ways to make you just do a good picture to become an animation on the MIDlet. First, put the pictures on a web server, let MIDLET download them, MIDP built-in HTTP support. The second method is simpler, put the picture with the MIDlet to packet into a JAR file. If you are using the J2ME development tool, put the PNG file in your project file. The animation process is actually more like account: displays the current frame and then replace it to the next frame. Then use a class to do this, it should be very appropriate, well, let's define an AnimatedImage class first:

Import java.util. *; import javax.microedition.lcdui. *; // Defines an animation that is actually just a series of picture // turnt display, then analoged animation 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 [] (null, images, null);}; // construct anion;}; // Construct anion;}; {; }; // construct an animation. The can not null the a repaint will be triggered // on it each time the Image Changes Due to a Timer // Event. IF a clip list isot isbook , 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 ....................... T = cliplist; if (images! = null && cliplist! = null) {; if (cliplist.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 CH = 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 NEXT Frame // and causes a repaint if a canvas isot () {; if (w == 0 || h == 0) Return; Advance (True);};}; you instantiate an AnimatedImage object, you must pass an Image object array for the configuration method of the AnimatedImage class, which represents each frame of the animation. All pictures used must have the same height and width. Load the image from the JAR file with image.createImage () method:

Private image [] loadframes (String name, int framees) throws ioException {; image [] images = new image [frames]; for (int i = 0; i zMBBS = 1; Because MIDP 1.0 does not support transparent pictures, an AnimatedImage class uses a clip list to simulate transparent results, and the clip list is a series of pictures that are cut. When the picture is drawn, you separate a few times, draw a clip area in a clip list. The clip list is defined based on the frame, so you need to create an array for each frame of the image. The size of the array should be a multiple of 4 because each clip area maintains four values: left coordinate, top coordinate, width, and height. The origin of the coordinates is the top left corner of the entire picture. It is necessary to pay attention to using the clip list to slow the animation. If the picture is more complicated, you should use a vector image. The AnimatedImage class extends java.util.timertask, allowing you to set a Timer. Here is an example of how to use Timer to do animation:

Timer Timer = new Timer (); AnimatedImage Ai = .....// get the image timer.schedule (AI, 200, 200); every approximately 200 milliseconds, Timer call animatedimage.run () method once, this method The animation is turned to the next frame. Now that we need is to let MIDlet to try the animation! We define a subclass of simple Canvas classes, so let us "past it".

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 ing.com ... if (! isdoublebuffred ()) {; offwidth = image.createImage (getWidth (), getHeight ());};}; // add an one 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.getgraph ICS ();}; g.setcolor (255, 255, 255); g.fillRect (0, 0, getWidth (), getHeight ()); int n = images.size (); for (int i = 0; i zmbs = 1; AnimateDcanvas class code is quite simple, by an animation import method and a PAINT method. Every time the Canvas can be drawn, the background will be erased and then looped each imported AnimatedImage object, painting directly to yourself (extended the Canvas class yourself).

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 NUM BER ... private int genrandom (INT UPPER) {; return (math.Abs ​​(random.nextint ())% UPPER);}; public display getDisplay () {; returnction;}; // initialize thing 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 [】 = loading, bird_frames); int w = c.Getwidth (); int h = c.getHeight (); birds = new animatedImage [Num_birds]; for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-97678.html

New Post(0)