Play and process of game frame animation for Android game development

Source: Internet
Author: User
Tags rar

Take you into the game development of the world game frame animation processing
<ignore_js_op>
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.

: 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.
<ignore_js_op>
2. Original files for animation resources

Original file for animation resources PNG generally has three forms of presentation please listen to me.



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.
<ignore_js_op>
2. All animation frames are present in a PNG image

A PNG contains all of 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 image will be played in the picture to be deducted from the screen to display on the phone. The arrangement of this kind of resources program needs to write the algorithm to calculate the coordinates of the picture.
<ignore_js_op>
3. Animation editor handles animations

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.

: <ignore_js_op>Auroragt.rar (1.51 MB, download count: 2046)
<ignore_js_op>
To everyone to see the resulting animation is very beautiful, it is not very to force it hehe.

<ignore_js_op>
<ignore_js_op>
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.

  1. Package Cn.m15.xys;
  2. Import Java.io.InputStream;
  3. Import Android.content.Context;
  4. Import Android.graphics.Bitmap;
  5. Import Android.graphics.BitmapFactory;
  6. Import Android.graphics.Canvas;
  7. Import Android.graphics.Paint;
  8. public class Animation {
  9. /** Last frame playback time **/
  10. Private long mlastplaytime = 0;
  11. /** play the ID of the current frame **/
  12. private int Mplayid = 0;
  13. /** Animated Frame Number **/
  14. private int mframecount = 0;
  15. /** for storing animated resource pictures **/
  16. Private bitmap[] Mframebitmap = null;
  17. /** whether the loop plays **/
  18. Private Boolean misloop = false;
  19. /** Play End **/
  20. Private Boolean misend = false;
  21. /** animation play Gap time **/
  22. private static final int anim_time = 100;
  23. /**
  24. * Constructor function
  25. * @param context
  26. * @param framebitmapid
  27. * @param isloop
  28. */
  29. Public Animation (context context, int [] Framebitmapid, Boolean isloop) {
  30. Mframecount = Framebitmapid.length;
  31. Mframebitmap = new Bitmap[mframecount];
  32. for (int i =0; i < Mframecount; i++) {
  33. Mframebitmap[i] = Readbitmap (Context,framebitmapid[i]);
  34. }
  35. Misloop = Isloop;
  36. }
  37. /**
  38. * Constructor function
  39. * @param context
  40. * @param framebitmap
  41. * @param isloop
  42. */
  43. Public Animation (context context, Bitmap [] Framebitmap, Boolean isloop) {
  44. Mframecount = Framebitmap.length;
  45. Mframebitmap = Framebitmap;
  46. Misloop = Isloop;
  47. }
  48. /**
  49. * Draw one of the frames in the animation
  50. * @param Canvas
  51. * @param paint
  52. * @param x
  53. * @param y
  54. * @param Frameid
  55. */
  56. public void Drawframe (canvas canvas, paint paint, int x, int y,int frameid) {
  57. Canvas.drawbitmap (Mframebitmap[frameid], x, y, paint);
  58. }
  59. /**
  60. * Draw Animations
  61. * @param Canvas
  62. * @param paint
  63. * @param x
  64. * @param y
  65. */
  66. public void drawanimation (canvas canvas, paint paint, int x, int y) {
  67. Resume playback If no playback is complete
  68. if (!misend) {
  69. Canvas.drawbitmap (Mframebitmap[mplayid], x, y, paint);
  70. Long time = System.currenttimemillis ();
  71. if (Time-mlastplaytime > Anim_time) {
  72. mplayid++;
  73. Mlastplaytime = time;
  74. if (Mplayid >= mframecount) {
  75. Logo animation play End
  76. Misend = true;
  77. if (Misloop) {
  78. Set Loop playback
  79. Misend = false;
  80. Mplayid = 0;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * Read Image Resources
  88. * @param context
  89. * @param resId
  90. * @return
  91. */
  92. Public Bitmap Readbitmap (context context, int resId) {
  93. Bitmapfactory.options opt = new bitmapfactory.options ();
  94. Opt.inpreferredconfig = Bitmap.Config.RGB_565;
  95. Opt.inpurgeable = true;
  96. Opt.ininputshareable = true;
  97. Get a picture of a resource
  98. InputStream is = Context.getresources (). Openrawresource (ResId);
  99. Return Bitmapfactory.decodestream (IS, null, opt);
  100. }
  101. }
Copy Code

Everybody look at my game demo use up and Down buttons to play up and down left to right character walking animation.
<ignore_js_op>
<ignore_js_op>
<ignore_js_op>
Finally because the code is more I do not post in the blog, the following gives the demo source of the Welcome to download reading each other to learn from each other, study
<ignore_js_op> Third-speaking frame animation. rar (426.78 KB, Downloads: 1354)

Ask.-Customized IT education platform, one-to-man service for cattle, questions and answers, development and programming social Headlines official website: www.wenaaa.com

QQ group 290551701 gathers a lot of Internet elite, technical director, architect, project Manager! Open source technology research, Welcome to the industry, Daniel and beginners are interested in engaging in IT industry personnel to enter!

Play and process of game frame animation for Android game development

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.