Android programming draws animation examples according to a series of pictures

This paper describes the method of drawing animation according to a series of pictures in Android programming. Share with you for your reference, as follows:

1、 Use the animation class provided by the system and the built-in method

The animation.xml file is as follows:

Where: / / set single play animationdrawable.setoneshot (true);

2、 Provides a lot of frame animation pictures, using Android drawing, you can draw the animation of characters walking.

As shown in the figure above, in this case, you can draw animation according to the following steps:

1. According to the character walking up, down, left and right, it can be divided into four animation segments, and an animation array with a length of 4 can be defined; 2. Different animations are triggered according to the events of the up, down, left and right keys on the keyboard.

The main drawn statements are as follows:

3、 If only one character picture is provided, the program shall be used to cut the picture to obtain the frame image required for animation,

As shown in the figure above, in this case, you can draw animation according to the following steps:

1. Through the width and height of the picture and the width and height of the defined tile, 12 required frame images can be cut; 2. According to the character walking up, down, left and right, it can be divided into four animation segments, and an animation array with a length of 4 can be defined; 3. Different animations are triggered according to the events of the up, down, left and right keys on the keyboard; The main codes for cutting pictures and drawing animation are as follows:

Note: the latter two methods use different drawing methods

The first drawing construction method is: animation (context, int [] framebitmapid, Boolean isloop);

The second drawing construction method is: animation (context, context, bitmap [] framebitmap, Boolean isloop);

With these preparation conditions, we can start real drawing:

Here, two flag bits are used to judge the state of the animation. Misend judges whether the animation is played. True ends the playback, and false is to play the animation; Misloop determines whether the animation is circular. True means circular and false means no circular; Of course, during the animation cycle, the animation must be played. Therefore, if misloop is true, then misend is false. Since each group of animation has four pictures, the mplayid is at most 4. When a character's group of actions, that is, an animation, is completed, it means that all four pictures have been drawn, then the mplayid should be set to 0, To start over, you need to draw four pictures to represent an animation.

How do we trigger animation by pressing keys in the main class? The OnDraw () method needs to be implemented and redrawn continuously. The main code is as follows:

In this way, our animation is going on all the time. We can control the sign by controlling the up, down, left and right buttons:

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>