Android Research game development frame animation implementation

Source: Internet
Author: User



1. The principle of frame animation

Frame animation frame animation as the name implies, a frame of a frame animation is a frame animation. Frame animation and our childhood to see the principle of the cartoon is the same, in the same area to quickly switch pictures to show people a visual illusion feel like in the play animation, in fact, is just n pictures in a frame of the switch. For the camera is not clear see Android Research game development Camera update

: The implementation of the character walking animation, 4-frame walking animation in the playing area, a frame to the left to play to give people an illusion of playing the animation, the picture is moving, very simple, the other three direction to play the animation method similar to I no longer one by one example. \ 2. Original file animation resource raw files for animated resources PNG generally has three forms of presentation please listen to me carefully. 1. Each frame is a PNG image up and down the direction of each group of animation each frame is a PNG picture, play animation need to switch the entire picture, to achieve animation effect. The code only need to complete the next frame of the picture to cover the previous frame of the picture is OK, this arrangement of resources in the program algorithm is the simplest. 2. All animation frames have a PNG image in a PNG picture of all the characters of the frame animation, play the animation when the program needs to calculate the image to be played in the original image of the starting and ending coordinates, that is, from the original picture will be playing the picture to be deducted, So that it appears on the phone screen. The arrangement of this kind of resources program needs to write the algorithm to calculate the coordinates of the picture. 3. Animation editor Processing animation game companies will have their own animation editor, the advantage of the animation editor is
1. Reduce the image size to save memory space
2. Shorten the art to coordinate time, because if there is no editor art is painful need a picture of a picture of the coordinates, all is physical activity.
3. Full data-driven animation, animation problem programs do not change code.       The bugs are all fine art, hehe. The animation editor is generated by coordinates that tell each point of the picture each dot of the animation to be stitched together each coordinate program needs to edit the generated coordinates to parse the resulting XML file from the animation editor and then draw the game animation. Auroragt Animation Editor is the author's most used animation editor It's very powerful to make any animation. I do not explain anything about the parsing and use of this editor for commercial use. If just want to study I put the editor of the post, we can study each other to discuss each other to learn from each other. SOURCE Download: Auroragt

to everyone to see the resulting animation is very beautiful, it is not very to force it hehe. I use the code to explain in detail the first and the second game animation code implementation method. I wrote an animation class myself to handle the animation, I need to call the animation only needs the new animation object to pass in the animation required parameters by calling the Drawanimation method to play the animation by the frame. If it is purely learning, I think this class is enough to learn to use.
<strong>package Cn.m15.xys;import Java.io.inputstream;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import    Android.graphics.paint;public class Animation {/** Previous frame playback time **/private long mlastplaytime = 0;    /** plays the ID of the current frame **/private int mplayid = 0;    /** Animated frame number **/private int mframecount = 0;    /** for storing animated resource pictures **/private bitmap[] Mframebitmap = null;    /** whether the loop plays **/private Boolean misloop = false;    /** Play ends **/private Boolean misend = false;    /** animation play Gap time **/private static final int anim_time = 100; /** * Constructor * @param context * @param framebitmapid * @param isloop */Public Animation (Context cont ext, int [] Framebitmapid, Boolean isloop) {mframecount = Framebitmapid.length;mframebitmap = new Bitmap[mframecount];for (int i =0; i < mframecount; i++) {Mframebitmap[i] = Readbitmap (Context,framebitmapid[i]);}    Misloop = Isloop; }   /** * Constructor * @param context * @param framebitmap * @param isloop */public Animation (context con Text, Bitmap [] Framebitmap, Boolean isloop) {mframecount = Framebitmap.length;mframebitmap = Framebitmap;misloop = Isloop    ; /** * Draw one of the frames in the animation * @param Canvas * @param paint * @param x * @param y * @param frameid */Publ IC void Drawframe (canvas canvas, paint paint, int x, int y,int frameid) {canvas.drawbitmap (Mframebitmap[frameid], X, Y, PA    int); /** * Animate * @param Canvas * @param paint * @param x * @param y */public void Drawanimati  On (canvas canvas, paint paint, int x, int y) {//If no play ends then continue to play if (!misend) {Canvas.drawbitmap (Mframebitmap[mplayid], X,    Y, paint);    Long time = System.currenttimemillis ();    if (Time-mlastplaytime > Anim_time) {mplayid++;mlastplaytime = time;if (Mplayid >= mframecount) {//Flag animation play End    Misend = true;    if (misloop) {//set loop playback misend = False;mplayid = 0; }}    }}}/** * Read Picture resource * @param context * @param resId * @return */public Bitmap Readbitmap (C Ontext context, int resId) {bitmapfactory.options opt = new bitmapfactory.options (); opt.inpreferredconfig = bitmap.config.rgb_565;opt.inpurgeable = True;opt.ininputshareable = true;//Get the resource picture inputstream is =    Context.getresources (). Openrawresource (resId); return Bitmapfactory.decodestream (IS, null, opt); }}</strong>
Everybody look at my game demo use up and Down buttons to play up and down left to right character walking animation.

Finally, because of the code more I do not post in the blog, the following gives the download of the demo source download Welcome to read each other to learn from each other, to discuss with each other and hope that we can progress together.

Source Download: Animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.