The example in this article describes how the Android program paints animations based on a series of pictures. Share to everyone for your reference, specific as follows:
First, using the system provided by the animation class, with the method of the band
The Animation.xml files are as follows:
<animation-list xmlns:android= "Http://schemas.android.com/apk/res/android" Android:oneshot= "false" > <item android:drawable= "@drawable/A" android:duration= "MB"/> android: Drawable= "@drawable/b" android:duration= "/>" <item android:drawable= "@drawable/C" android:duration= "100" > <item android:drawable= "@drawable/d" android:duration= "MB"/> <item android:drawable= "@drawable/e" and Roid:duration= "/>" <item android:drawable= "@drawable/F" android:duration= "/> <item Android:drawa ble= "@drawable/g" android:duration= "/> <item android:drawable=" @drawable/h "android:duration="/> " Lt;item android:drawable= "@drawable/I" android:duration= "MB"/> <item android:drawable= "@drawable/j" Android: Duration= "/>" </animation-list>
Animationdrawable animationdrawable = null;
/** get ImageView object **/
ImageView = (imageview) Findviewbyid (R.id.imageview);
/** animationdrawable**/
animationdrawable = (animationdrawable) imageview.getbackground () by ImageView object to the background display ;
/** begins to play the animated **/
button0 = (Button) Findviewbyid (r.id.button0);
Button0.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View arg0) {
/** play animation * /
if (!animationdrawable.isrunning ()) {
animationdrawable.start ();
}}}
);
Where://Set a single play Animationdrawable.setoneshot (true);
Two, provides many frame animation picture, utilizes the Android drawing, may draw the character to move around the animation.
As shown above, in this case, you can draw the animation as follows:
1, according to people walking around, can be divided into four animation, you can define a length of 4 animation array;
2, according to the keyboard up and down key events respectively trigger different animation.
The main drawn statements are as follows:
Mheroanim[anim_down] = new Animation (context,new int []{r.drawable.hero_down_a,r.drawable.hero_down_b, R.drawable.hero_down_c,r.drawable.hero_down_d},true);
Mheroanim[anim_left] = new Animation (context,new int []{r.drawable.hero_left_a,r.drawable.hero_left_b, R.drawable.hero_left_c,r.drawable.hero_left_d},true);
mheroanim[anim_right]= new Animation (context,new int []{r.drawable.hero_right_a,r.drawable.hero_right_b, R.drawable.hero_right_c,r.drawable.hero_right_d},true);
MHEROANIM[ANIM_UP] = new Animation (context,new int []{r.drawable.hero_up_a,r.drawable.hero_up_b,r.drawable.hero_up_ C,r.drawable.hero_up_d},true);
Third, only provide a picture of the people, we should use the program to cut the picture, get the animation required frame image,
As shown above, in this case, you can draw the animation as follows:
1, through the width and height of the picture, and the definition of the width and height of the tile, you can cut out 12 pieces of the required frame image;
2, according to people walking around, can be divided into four animation, you can define a length of 4 animation array;
3, according to the keyboard up and down key events trigger different animation respectively;
The main code to cut the picture and draw the animation is as follows:
//using the program to cut the picture Bitmap TestMap = Readbitmap (Context,r.drawable.enemy);
Bitmap [][]bitmap = new Bitmap[anim_count][anim_count];
int tilewidth = Testmap.getwidth ()/anim_count;
int tileheight = Testmap.getheight ()/anim_count;
int i = 0,x = 0,y = 0;
For (i =0 i < Anim_count; i++) {y = 0;
Bitmap[anim_down][i] = Bitmapclipbitmap (testmap,x,y,tilewidth,tileheight);
Y+=tileheight;
Bitmap[anim_left][i] = Bitmapclipbitmap (testmap,x,y,tilewidth,tileheight);
Y+=tileheight;
Bitmap[anim_right][i] = Bitmapclipbitmap (testmap,x,y,tilewidth,tileheight);
Y+=tileheight;
Bitmap[anim_up][i] = Bitmapclipbitmap (testmap,x,y,tilewidth,tileheight);
x+= Tilewidth;
} Mtestanim[anim_down] = new Animation (context,bitmap[anim_down],true);
Mtestanim[anim_left] = new Animation (context,bitmap[anim_left],true);
mtestanim[anim_right]= new Animation (context,bitmap[anim_right],true);
MTESTANIM[ANIM_UP] = new Animation (context,bitmap[anim_up],true);
Note: The above two methods are used in different drawing methods respectively.
The first method of drawing construction is: Animation (context context, int [] Framebitmapid, Boolean isloop);
The second method of drawing construction is: Animation (context context, Bitmap [] Framebitmap, Boolean isloop);
With these prep conditions, we can start the real drawing:
public void Drawanimation (Canvas Canvas, Paint Paint, int x, int y) {
//continued playing if
(!misend) {canvas.dr If no playback ended
Awbitmap (Mframebitmap[mplayid], x, y, paint);
Long time = System.currenttimemillis ();
if (Time-mlastplaytime > Anim_time) {
mplayid++;
Mlastplaytime = time;
if (Mplayid >= mframecount) {
//Flag animation playback End
Misend = true;
if (misloop) {
//Set loop playback
misend = false;
Mplayid = 0;
}}}
Here two flags are used to determine the state of the animation, misend to determine whether the animation play, True end play, false to play animation, Misloop to determine whether the animation loop, true to loop, false is not loop; Of course, when the animation loops, the animation is definitely played, So Misloop is true, then Misend is false, because each group of animation is four pictures, so the Mplayid up to 4, when the characters a group of action is an animation completed, said 4 pictures are drawn once, then to speak Mplayid to 0, Indicates that you need to draw four pictures to represent an animation again.
How do we trigger animations in the main class by pressing the key? You need to implement the OnDraw () method and continually redraw the main code as follows:
protected void OnDraw (Canvas Canvas) {canvas.drawbitmap (Mmapimage, 0,0, Mpaint);
Canvas.save ();
Canvas.cliprect (0, 0,320, 30);
Mpaint.setcolor (Color.White);
Canvas.drawrect (0, 0,480, mpaint);
Mpaint.setcolor (color.red);
Canvas.restore ();
/** to update the display animation **/if (Mallkeydown) {if (Miskeydown) {manimationstate = Anim_down;
Canvas.drawtext ("Press the next key, start to play downward animation start", 0, Mpaint);
else if (miskeyleft) {manimationstate = Anim_left;
Canvas.drawtext ("Press left button, start playing left animation start", 0, Mpaint);
else if (miskeyright) {manimationstate = Anim_right;
Canvas.drawtext ("Press right button, start playing right animation start", 0, Mpaint);
else if (miskeyup) {manimationstate = anim_up;
Canvas.drawtext ("Press the top button, start to play up animation start", 0, Mpaint); /** draws the protagonist animation **/Mheroanim[manimationstate].
Drawanimation (Canvas, Mpaint, 20, 100); Mtestanim[manimationstate].
Drawanimation (canvas, Mpaint, 100, 100); }else {/** button lifted after the character stop animation **/Mheroanim[manimationstate].
Drawframe (Canvas, Mpaint, 20, 100, 0); MTestanim[manimationstate].
Drawframe (canvas, Mpaint, 100, 100, 0);
Canvas.drawtext ("button has been lifted animation stop", 0, Mpaint);
} super.ondraw (canvas);
Invalidate ();
}
In this way, our animation drawing is always in progress.
We can control the flag by controlling the top and Bottom buttons:
public void setkeystate (int keycode, Boolean state) {
switch (keycode) {case
Keyevent.keycode_dpad_down:
Miskeydown = State;
break;
Case KEYEVENT.KEYCODE_DPAD_UP:
miskeyup = State;
break;
Case Keyevent.keycode_dpad_left:
miskeyleft = State;
break;
Case Keyevent.keycode_dpad_right:
miskeyright = State;
break;
Mallkeydown = State;
}
I hope this article will help you with the Android program.