Implement animation effects based on MIDP1.0

xiaoxiao2021-03-06  82

Inside the SUN's technical article, there is an article tells how to implement animations based on MIDP1.0, and individuals feel very good. In MIDP 1.0 and support the animation directly, if we understand the motion of the animation, familiar with Timer and Timertask are not very difficult.

The animation is actually a series of continuous frames. When they transform enough time, our human eyes will feel that he is moving. So if we can periodically switch screens, from the first frame to the second frame to the third frame ... then we can make an animation effect. Timer and Timertask can help us with this feature, if you are not familiar with these two classes, you can refer to Timer and Timertask in Java. We first expand Timertask to implement an AnimatedImage class, which is the functionality to picture java.util. *; Import javax.microedition.lcdui. *;

// defines An animated image, Which is Just a set // of images in turn // to Simulate Movement.

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 ") {this (null, images, null);}

// Construct an animation with a null clip list.

Public AnimatedImage (canvas canvas, image [] images) {this (canvas, images, null);}

// Construct anion. The Canvas Can Be Null, But // IF Null THEN A Repaint Will Be Triggered On It // Each Time The Image Changes Due To a Timer Event. //Iff 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.cliplist = cliplist;

IF (images! = null && cliplist! = null) {if (cliplist.Length

//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 ();}}

.

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);}}

We need to store several sizes in the JAR file, and the shape is ordered, which is loaded in an image array when the MIDlet is started, and passes it as a parameter to animatedimage. Private image [] loadframes (string name, int frames) throws ioException {image [] iMages = new image [frame "; for (int i = 0; i

Return images;

Image [] frames = loadingframes ("/ images / bird", 7); AnimatedImage Ai = New AnimatedImage (frame); Ai.move (20, 20); // set top-left to 20, 20

Below is the source code for the AnimateDcanvas and AnimationTest classes of the two test results, where you can get all source files and image resources. Reading this class carefully is very good. One thing is worth mentioning that when drawing pictures, it should be determined whether the device supports dual buffering, if not supported to use dual buffer technology to avoid screen blinking. And we are also not suitable for excessive animation pictures, which will increase the size of the JAR file, and will cause a big burden to the equipment.

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

IF (g! = saved) {saved.drawimage (OFFSCREEN, 0, 0, Graphics.Left | graphics.top);}}}}}} import java.io. *; import java.util. *; import javax.microedition.lcdui. *; import javax.microedition.midlet. *;

// MIDlet That Displays a Series of Birds on the screen and // Animates Them at Different (Random) Rates.public class AnimationTest Extends MIDlet imports 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 MIDLETSTATECHANGEXCEPTION {EXITMIDLET ();

Public void exitmidlet () {timer.cancel (); // Turn it off ... notifydestroyed ();

// generate a non-negative random number ...

Private Int genrandom (INT UPPER) {Return (Math.Abs ​​(Random.nextINT ())% UPPER);}

Public Display getDisplay () {Return Display;

// Initialize Things by Creating The Can Birds THEN // Creat To // Random Locations on The Canvas and Attached to // A Timer for Scheduling.

Protected void initmidlet () {Try {Animatedcanvas C = new animatedcanvas (getDisplay ()); image [] images = loadingframes ("/ images / bird", bird_frames);

INT W = C.GetWidth (); int h = c.getHeight ();

Birds = new animatedimage [num_birds]; for (int i = 0; i

GetDisplay (). setCurrent (c);} catch (ioException e) {system.out.println ("could not load images"); 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 ioException {image [] iMages = new image [frame "; for (int i = 0; i

Return images;

protected void pauseapp () {}

protected void startApp () throws MIDletStateChangeException {if (display == null) {display = Display.getDisplay (this); initMIDlet ();}}} import java.util *;. import javax.microedition.lcdui *.;

// a canvas to which you can attach one or more on a............................

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 (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

IF (g! = saved) {saved.drawimage (offscreen, 0, 0, graphics.LRAPHICS.TOP);}}}

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

New Post(0)