Animations are divided into Frame-by-Frame, Tween, and attribute animations.
1. frame-by-frame animation
XML resource files are usually used for definition as follows:
Android: oneshot: sets whether to play the animation cyclically. false indicates that loop playback is the default setting. The xml file defines the Frame Animation resource. You can use an ImageView in the program to display the animation.
It should be pointed out that the animation represented by AnimationDrawable is not played by default. It needs to be started in the program and start () and stop () are called ().
Example of bullet explosion effect
Import java. lang. reflect. field; import android. app. activity; import android. content. context; import android. graphics. canvas; import android. graphics. drawable. animationDrawable; import android. media. mediaPlayer; import android. OS. bundle; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. frameLayout; import android. widget. imageView; public class Blast extends Activity {private MyView myView; private AnimationDrawable anim; private MediaPlayer bomb; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // use the FrameLayout layout manager, which allows the component to control its position FrameLayout frame = new FrameLayout (this); setContentView (frame); // sets the background frame. setBackgroundResource (R. drawable. back); // load the sound bomb = MediaPlayer. create (this, R. raw. bomb); myView = new MyView (this); // set myView to display the blast animation myView. setBackgroundResource (R. anim. blast); // set myView to hide myView by default. setVisibility (View. INVISIBLE); // get the animation object anim = (AnimationDrawable) myView. getBackground (); frame. addView (myView); frame. setOnTouchListener (new OnTouchListener () {public boolean onTouch (View source, MotionEvent event) {// only process the pressed event (avoid generating two animation effects each time) if (event. getAction () = MotionEvent. ACTION_DOWN) {// stop playing anim first. stop (); float x = event. getX (); float y = event. getY (); // control the display position of myView. setLocation (int) y-40, (int) x-20); myView. setVisibility (View. VISIBLE); // start the anim animation. start (); // play the sound bomb. start () ;}return false ;}}) ;}// define a custom View, this custom View is used to play the "explosion" effect class MyView extends ImageView {public MyView (Context context) {super (context) ;}// define a method, this method is used to control the display position of MyView public void setLocation (int top, int left) {this. setFrame (left, top, left + 40, top + 40);} // rewrite this method to control if the animation is played to the last frame, hide this Viewprotected void onDraw (Canvas canvas) {try {Field field = AnimationDrawable. class. getDeclaredField ("mCurFrame"); field. setAccessible (true); // get the current anim animation frame int curFrame = field. getInt (anim); // if the last frame has been reached if (curFrame = anim. getNumberOfFrames ()-1) {// hide setVisibility (View. INVISIBLE) ;}} catch (Exception e) {} super. onDraw (canvas );}}}Blast. xml