Android Research Institute Game Development Frame Animation (3) Release 4. 28,201 2 Category [Game Development of Android Research Institute] If you need to reprint the original article, please note: Reprinted from Yusong Momo Program research institute this article link address: Game Development Frame Animation of Android Research Institute (iii)
1. Principles of Frame Animation
Frame Animation, as its name implies, is a frame animation. Frame Animation works the same way as the cartoon we watched when we were a child. Switching images in the same area quickly gives people the illusion that they are playing an animation, in fact, n images are switched between one frame and one frame.
: How to implement a walking animation for a character. When a four-frame walking animation is in the playing area, one frame switches to the left to give people the illusion of playing an animation, and the picture moves, the other three-direction method for playing an animation is similar.
2. There are three ways to render the original file PNG of the original file of the animation resource.
1. Each frame is in a PNG image.
Each frame in each group of animations is a PNG Image in the upper, lower, and left directions. To play an animation, you must switch the entire image to achieve the animation effect. In the code, you only need to completely overwrite the image of the next frame to the image of the previous frame. This resource arrangement method is the simplest in programming algorithms.
2. all animation frames exist in one PNG image, and one PNG contains all the animation frames of characters, when playing an animation, the program needs to calculate the starting and ending coordinates of the image to be played in the original image, that is, to deduct the image to be played from the original image, this is displayed on the mobile phone screen. In this way, the program needs to compile an algorithm for calculating the Coordinate Position of an image.
3. animation editor processing animation game companies will all have their own animation editors. The advantages of the animation editor are:
1. Reduce image size and save memory space
2. shorten the time for the artist to coordinate, because if there is no editor artist, it is very painful to need the coordinates of an image and an image, it is all physical work.
3. Full data-driven animation. If there is an animation problem, the program does not need to change the code. Bug is all art, huh, huh.
In fact, the animation editor generates coordinates to tell the image each point. Each coordinate program concatenates each animation point. You need to edit the XML file generated by the animation editor and parse it according to the generated coordinates. draw game animations. The auroragt animation editor is the most used animation editor. It is very powerful and can compile any animation. I will not explain the parsing and use of this editor for commercial purposes. If you just want to learn how to paste the editor, you can study and discuss with each other.
: Http://download.csdn.net/source/3462935
Let's see if the generated animation is brilliant. Is it awesome.
I will use the code to describe the code implementation methods of the first and second game animations in detail.
I wrote an animation class to process the playback of the animation. To call the animation, you only need to call the parameters required for the new animation object to pass in the animation. by calling the drawanimation method, you can play the animation by frame. If it is just learning, I think this class is enough to learn and use.
001 package CN. m15.xys; 002 003 import Java. io. inputstream; 004 005 import android. content. context; 006 import android. graphics. bitmap; 007 import android. graphics. bitmapfactory; 008 import android. graphics. canvas; 009 import android. graphics. paint; 010 011 public class animation {012 013/** last frame playback time **/014 private long mlastplaytime = 0; 015/** ID of the current frame to be played **/016 private int mplayid = 0; 017/** Number of animated frames **/018 PRI Vate int mframecount = 0; 019/** used to store animation resource images **/020 private bitmap [] mframebitmap = NULL; 021/** loop playback **/022 private Boolean misloop = false; 023/** end of playback **/024 private Boolean misend = false; 025/** animation playback gap time **/026 Private Static final int anim_time = 100; 027 028/** 029 * constructor 030 * @ Param context031 * @ Param framebitmapid032 * @ Param isloop033 */034 public animation (context, int [] Frame Bitmapid, Boolean isloop) {035 mframecount = framebitmapid. length; 036 mframebitmap = new bitmap [mframecount]; 037 for (INT I = 0; I <mframecount; I ++) {038 mframebitmap [I] = readbitmap (context, framebitmapid [I]); 039} 040 misloop = isloop; 041} 042 043/** 044 * constructor 045 * @ Param context046 * @ Param framebitmap047 * @ Param isloop048 */049 public animation (context, bitmap [] framebitmap, Boolean I Sloop) {050 mframecount = framebitmap. length; 051 mframebitmap = framebitmap; 052 misloop = isloop; 053} 054 055/** 056 * One of the frames in the animation is 057 * @ Param canvas058 * @ Param paint059 * @ Param x060 * @ Param y061 * @ Param frameid062 */063 public void drawframe (canvas, paint paint, int X, int y, int frameid) {064 canvas. drawbitmap (mframebitmap [frameid], X, Y, paint); 065} 066 067/** 068 * draw animation 069 * @ Param ca Nvas070 * @ Param paint071 * @ Param x072 * @ Param y073 */074 public void drawanimation (canvas, paint, int X, int y) {075 // continue playing 076 if (! Misend) {077 canvas. drawbitmap (mframebitmap [mplayid], X, Y, paint); 078 long time = system. currenttimemillis (); 079 if (time-mlastplaytime> anim_time) {080 mplayid ++; 081 mlastplaytime = time; 082 if (mplayid> = mframecount) {083 // indicates that the animation playback ends. 084 misend = true; 085 if (misloop) {086 // sets the loop playback 087 misend = false; 088 mplayid = 0; 089} 090} 091} 092} 093} 094 095/** 096 * read image resource 097 * @ Param context098 * @ Param resid099 * @ return100 */101 public bitmap readbitmap (Context context, int resid) {102 bitmapfactory. options opt = new bitmapfactory. options (); 103 OPT. inpreferredconfig = bitmap. config. rgb_565; 104 OPT. inpurgeable = true; 105 OPT. ininput0000able = true; 106 // obtain the resource image 107 inputstream is = context. getresources (). openrawresource (resid); 108 return bitmapfactory. decodestream (is, null, OPT); 109} 110}
Let's take a look at my game demo and use the top, bottom, left, and right buttons to play an animation of walking up and down to the right.
Finally, I will not post my blog because of the large amount of code. The demo source code is provided below. You are welcome to download, read, and study each other, and discuss with each other. Yu Song Momo hopes to make progress together with you.
: Http://vdisk.weibo.com/s/aa9jR