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>
Next 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;
- Public class BubbleExplosion extends Activity {
- Private FrameLayout fl;
- Private ExplosionView exv1;
- Private AnimationDrawable exa1;
- Public void 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 context ){
- Super (context );
- }
- // Handle the explosion location
- Public void setLocation (int top, int left ){
- This. setFrame (left, top, left + 40, top + 40 );
- }
- }
- Class LayoutListener implements OnTouchListener {
- Public boolean 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 ();
- Return false;
- }
- }
- }
With SurfaceView of Android, Animation can achieve a good transitional effect, and SurfaceView is easy to use.