During the development of magicbubble In the android game, when two buckets are connected, the bubble disappears in the case of bubbles. The idea of the author is as follows: add an imageview to framelayout and define an explosion of animation. imageview is hidden when it is not needed, move the imageview to the desired location and then startanimation to achieve the explosive effect. The following is the simplified code of the program. The effect of the program is as follows: Click anywhere on the screen to display the explosion effect.
The first is the definition of animation, which defines a frame animation and plays 5 animations in sequence. The duration of each animation is 50 milliseconds:
<Animation-list xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: oneshot = "true">
<Item Android: drawable = "@ drawable/explode1" Android: Duration = "50"/>
<Item Android: drawable = "@ drawable/explode2" Android: Duration = "50"/>
<Item Android: drawable = "@ drawable/explode3" Android: Duration = "50"/>
<Item Android: drawable = "@ drawable/explode4" Android: Duration = "50"/>
<Item Android: drawable = "@ drawable/explode5" Android: Duration = "50"/>
</Animation-List>
The following is the main program code: Package com. Ray. Bubble;
Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. drawable. animationdrawable;
Import Android. OS. Bundle;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. View. View. ontouchlistener;
Import Android. widget. framelayout;
Import Android. widget. imageview;
Publicclass bubbleexplosion extends activity {
Private framelayout FL;
Private explosionview exv1;
Private animationdrawable exa1;
Publicvoid oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Set full screen
Requestwindowfeature (window. feature_no_title );
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
Windowmanager. layoutparams. flag_fullscreen );
FL = new framelayout (this );
Fl. setbackgroundresource (R. drawable. bg );
Exv1 = new explosionview (this );
Exv1.setvisibility (view. Invisible );
Exv1.setbackgroundresource (R. anim. EXPLOSION );
Exa1 = (animationdrawable) exv1.getbackground ();
Fl. addview (exv1 );
Fl. setontouchlistener (New layoutlistener ());
Setcontentview (FL );
}
Class explosionview extends imageview {
Public explosionview (context ){
Super (context );
}
// Handle the explosion location
Publicvoid setlocation (INT top, int left ){
This. setframe (left, top, left + 40, top + 40 );
}
}
Class layoutlistener implements ontouchlistener {
Publicboolean ontouch (view V, motionevent event ){
// First, you must stop playing the animation. If the animation starts, you cannot repeat it!
Exv1.setvisibility (view. Invisible );
Exa1.stop ();
Float x = event. getx ();
Float y = event. Gety ();
Exv1.setlocation (INT) y-20, (INT) x-20 );
Exv1.setvisibility (view. Visible );
Exa1.start ();
Returnfalse;
}
}
}
With surfaceview of Android, animation can achieve a good transitional effect, and surfaceview is easy to use.