直接上代码把,上面都写了很清楚的注释:
还有重要的就是这个anim文件夹中的explosion.xml
- 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 parentLayout;
- private ExplosionView customView;
- private AnimationDrawable exa1;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置无标题栏,全屏效果
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- // 代码创建一个FrameLayout,设置背景图片
- parentLayout = new FrameLayout(this);
- parentLayout.setBackgroundResource(R.drawable.bg);
- // 代码创建一个自定义ImageView,并添加到上面的FrameLayout当中
- // 并且设置这个ImageView有个动画的背景效果,
- // 设置为不可见
- customView = new ExplosionView(this);
- customView.setVisibility(View.INVISIBLE);
- customView.setBackgroundResource(R.anim.explosion);
- // 获取动画效果背景的图像
- exa1 = (AnimationDrawable) customView.getBackground();
- parentLayout.addView(customView);
- // 设置页面点击监听
- parentLayout.setOnTouchListener(new LayoutListener());
- setContentView(parentLayout);
- }
- class ExplosionView extends ImageView {
- public ExplosionView(Context context) {
- super(context);
- }
- // handle the location of the explosion
- 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 u have to stop the animation,or if the animation
- // is starting ,u can start it again!
- customView.setVisibility(View.INVISIBLE);
- // 如果动画已在运行,则不起效果
- exa1.stop();
- float x = event.getX();
- float y = event.getY();
- // 重新定位ImageView在layout上的坐标位置
- customView.setLocation((int) y - 20, (int) x - 20);
- customView.setVisibility(View.VISIBLE);
- exa1.start();
- return false;
- }
- }
- }
还有重要的就是这个anim文件夹中的explosion.xml
- <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" />
- </animation-list>
浙公网安备 33010602011771号