I saw a half-day code, I didn't understand, a lot of research ~
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 in development), 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. *;
// Define an animation, which is actually just a series of pictures of the same size.
// Turning display, then simulate the animation
Public class AnimatedImage Extends Timertask {;
Private Canvas Canvas;
PRIVATE [] ]MAGES;
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 []) {;
THIS (NULL, IMAGES, NULL);
}
// Construct an animation with a null clip list.
Public AnimatedImage (Canvas Canvas, Image []
{; this (canvas, images, null);
}
// construct an animation. The Canvas Can Be Null,
// But if not null the a repaint will be triggered
// on it each time the Image change Due to a Timer
// Event. if a clip list isot, the image is
// DRAWN MULTIPLE TIMES, EACH TIME WITH A DIFFERENT
// Clip Rectangle, To Simulate Transparent Parts.
Public AnimatedImage (Canvas Canvas, Image ",
int [] cliplist) {; this.canvas = canvas;
..
THIS.CLIPLIST = CLIPLIST;
IF (images! = null && cliplist! = NULL) {;
CLIPLIST.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 recctangle acidingly 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 time. Advances to the next frame // and causes a repaint if a canvas isot specified. Public void Run () {; if (w == 0 || h == 0) Return; Advance (True); } } When you instantiate an AnimatedImage object, you must pass a configuration method for the AnimatedImage class to an array of image object, 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 frames) Throws oException {; Image [] images = new image [frames]; For (int i = 0; i Images = image.createImage (Name i ".png"); } Return images; } You can also pass a Canvas object (optional), and a clip list (Clip List). If you specify a CANVAS and use a Timer to move to the next frame of the animation, as in the example code below, Canvas automatically retrans in the animation. However, this implementation is optional, you can do this, you can also let the program choose to be appropriate to redraw CANVAS. 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 pieces of blocks 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 calls an animatedimage.run () method once, this method makes the animation roll back 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 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 Ourslves ... 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. Double. Double IMAGE // buffering is buy to reduce flicker. Protected Void Paint (Graphics G) {; Graphics saved = g; IF (OFFSCREEN! = null) {; g = OFFSCREEN.GRAPHES (); } G.SetColor (255, 255, 255); G.fillRect (0, 0, getWidth (), getHeight ()); INT N = images.size (); For (int i = 0; i AnimatedImage IMG = (AnimatedImage) Images.ementat (i); Img.draw (g); } IF (g! = saved) {; Saved.drawImage (offscreen, 0, 0, Graphics.top | graphics.top); } } } The code of the AnimatedCanvas class is quite simple, and is imported by an animation 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 Animation. // 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 statin final int num_birds = 5; Private display display; Private Timer Timer = New Timer (); Private animatedimage [] birds; PRIVATE 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 MIDLETSTATECHANGEXCEPTION {; exitmidlet (); } Public void exitmidlet () {; Timer.cancel (); // Turn IT Off ... NotifyDestroyed (); } // generate a non-negative random number ... Private int genrandom (int UPER) {; 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 location 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 AnimatedImage B = New AnimatedImage (C, Images); BIRDS = B; B.Move (Genrandom (W), Genrandom (H)); C.Add (b); Timer.schedule (B, Genrandom (1000), Genrandom (400)); } C.addcommand (exitcommand); C.setCommandListener (this); GetDisplay (). setcurrent (c); } Catch (IOException E) {; System.out.println ("COULD NOT "); exitmidlet (); } } // load the bird animation, Which is stored as a // Series of png files in the midlet suite. Private image [] loadframes (string name, int frames) Throws oException {; Image [] images = new image [frames]; For (int i = 0; i i ".png"); } Return images; } Protected void pauseapp () {; } protected void startapp () THROWS MIDLETSTATECHANGEXCEPTION {; IF (display == null) {; Display = display.getdisplay (this); InitmIDlet (); } } } The animation of the seven frames, you can see a bird that takes wings. The MIDlet showed 5 birds, the position and refresh rate of the birds were random. You can use some other way to improve this program, but this program should be enough to let you get started.