在做Android游戏MagicBubble开发的时候,在连通两个Bubbles的时候,Bubble会以水泡爆破的情形消
失。笔者的思路是这样的:在FrameLayout里面加入一ImageView,再定义一个爆炸的Animation,不需
要的时候,ImageView就隐藏起来,需要的时候,就把ImageView移动到需要的地方,再StartAnimation,
这样,就可以实现爆炸效果。下面是简化后的程序的代码,程序的效果如下:点中屏幕中任意地方,就在点击
地方显示爆炸效果。
首先是Animation的定义,定义一个Frame Animation,依次播放5帧动画,每帧动画持续时间为50毫秒:
<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>
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>
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 context) {
super(context);
}
// 处理爆炸的位置
publicvoid setLocation(int top,int left){
this.setFrame(left, top, left+40, top+40);
}
}
class LayoutListener implements OnTouchListener{
publicboolean onTouch(View v, MotionEvent event) {
//首先,你必须停止播放动画,如果动画开始,你不能重复一遍!
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;
}
}
}
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 context) {
super(context);
}
// 处理爆炸的位置
publicvoid setLocation(int top,int left){
this.setFrame(left, top, left+40, top+40);
}
}
class LayoutListener implements OnTouchListener{
publicboolean onTouch(View v, MotionEvent event) {
//首先,你必须停止播放动画,如果动画开始,你不能重复一遍!
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;
}
}
}
配合Android的SurfaceView,Animation可以实现很好的过渡效果,SurfaceView的用法很简单。