直接上代码把,上面都写了很清楚的注释: 
Java代码 
  1. package com.ray.bubble;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.drawable.AnimationDrawable;  
  6. import android.os.Bundle;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.Window;  
  10. import android.view.WindowManager;  
  11. import android.view.View.OnTouchListener;  
  12. import android.widget.FrameLayout;  
  13. import android.widget.ImageView;  
  14.   
  15. public class BubbleExplosion extends Activity {  
  16.     private FrameLayout parentLayout;  
  17.     private ExplosionView customView;  
  18.     private AnimationDrawable exa1;  
  19.   
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         // 设置无标题栏,全屏效果  
  23.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  24.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  25.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  26.           
  27.         // 代码创建一个FrameLayout,设置背景图片  
  28.         parentLayout = new FrameLayout(this);  
  29.         parentLayout.setBackgroundResource(R.drawable.bg);  
  30.           
  31.         // 代码创建一个自定义ImageView,并添加到上面的FrameLayout当中  
  32.         // 并且设置这个ImageView有个动画的背景效果,  
  33.         // 设置为不可见  
  34.         customView = new ExplosionView(this);  
  35.         customView.setVisibility(View.INVISIBLE);  
  36.         customView.setBackgroundResource(R.anim.explosion);  
  37.           
  38.         // 获取动画效果背景的图像  
  39.         exa1 = (AnimationDrawable) customView.getBackground();  
  40.           
  41.         parentLayout.addView(customView);  
  42.         // 设置页面点击监听  
  43.         parentLayout.setOnTouchListener(new LayoutListener());  
  44.         setContentView(parentLayout);  
  45.     }  
  46.   
  47.     class ExplosionView extends ImageView {  
  48.         public ExplosionView(Context context) {  
  49.             super(context);  
  50.         }  
  51.   
  52.         // handle the location of the explosion  
  53.         public void setLocation(int top, int left) {  
  54.             this.setFrame(left, top, left + 40, top + 40);  
  55.         }  
  56.     }  
  57.   
  58.     class LayoutListener implements OnTouchListener {  
  59.         public boolean onTouch(View v, MotionEvent event) {  
  60.             // first u have to stop the animation,or if the animation  
  61.             // is starting ,u can start it again!  
  62.             customView.setVisibility(View.INVISIBLE);  
  63.              // 如果动画已在运行,则不起效果  
  64.             exa1.stop();  
  65.             float x = event.getX();  
  66.             float y = event.getY();  
  67.             // 重新定位ImageView在layout上的坐标位置  
  68.             customView.setLocation((int) y - 20, (int) x - 20);  
  69.             customView.setVisibility(View.VISIBLE);  
  70.             exa1.start();  
  71.             return false;  
  72.         }  
  73.   
  74.     }  
  75. }  

还有重要的就是这个anim文件夹中的explosion.xml 
Java代码 
  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:oneshot="true">  
  3.     <item android:drawable="@drawable/explode1" android:duration="50" />  
  4.     <item android:drawable="@drawable/explode2" android:duration="50" />  
  5.     <item android:drawable="@drawable/explode3" android:duration="50" />  
  6.     <item android:drawable="@drawable/explode4" android:duration="50" />  
  7. </animation-list>  

posted on 2011-04-15 09:27  kitea  阅读(132)  评论(0)    收藏  举报