Android游戏开发之爆炸效果

本文介绍了一种在Android游戏中实现水泡爆炸效果的方法。通过使用FrameLayout和ImageView结合自定义Animation,作者成功创建了一个可重复使用的爆炸动画组件。文章提供了完整的代码示例,并解释了如何在屏幕上指定位置触发爆炸动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在做Android游戏MagicBubble开发的时候,在连通两个Bubbles的时候,Bubble会以水泡爆破的情形消失。为了实现这一效果,我查找了不少资料,希望能找到一些标准的实现方面,花了不少时间,发觉Android关于游戏开发的资料实在太少了,更不用说标准做法了,没办法,只能按照自己的思路来实现这一效果。

我的思路是这样的(仅供参考,希望有更好做法的朋友跟我们共享一下):在FrameLayout里面加入一ImageView,再定义一个爆炸的Animation,不需要的时候,ImageView就隐藏起来,需要的时候,就把ImageView移动到需要的地方,再StartAnimation,这样,就可以实现爆炸的效果。

下面是简化后的程序的代码,程序的效果如下:点中屏幕中任意地方,就在点击地方显示爆炸效果。

首先是Animation的定义,定义一个Frame Animation,依次播放5帧动画,每帧动画持续时间为50毫秒:

Xml代码 复制代码
  1. <animation-listxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:oneshot="true">
  3. <itemandroid:drawable="@drawable/explode1"android:duration="50"/>
  4. <itemandroid:drawable="@drawable/explode2"android:duration="50"/>
  5. <itemandroid:drawable="@drawable/explode3"android:duration="50"/>
  6. <itemandroid:drawable="@drawable/explode4"android:duration="50"/>
  7. <itemandroid:drawable="@drawable/explode5"android:duration="50"/>
  8. </animation-list>
<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>

接着是主程序代码:

Java代码 复制代码
  1. packagecom.ray.bubble;
  2. importandroid.app.Activity;
  3. importandroid.content.Context;
  4. importandroid.graphics.drawable.AnimationDrawable;
  5. importandroid.os.Bundle;
  6. importandroid.view.MotionEvent;
  7. importandroid.view.View;
  8. importandroid.view.Window;
  9. importandroid.view.WindowManager;
  10. importandroid.view.View.OnTouchListener;
  11. importandroid.widget.FrameLayout;
  12. importandroid.widget.ImageView;
  13. publicclassBubbleExplosionextendsActivity{
  14. privateFrameLayoutfl;
  15. privateExplosionViewexv1;
  16. privateAnimationDrawableexa1;
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. //setfullscreen
  20. requestWindowFeature(Window.FEATURE_NO_TITLE);
  21. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  22. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  23. fl=newFrameLayout(this);
  24. fl.setBackgroundResource(R.drawable.bg);
  25. exv1=newExplosionView(this);
  26. exv1.setVisibility(View.INVISIBLE);
  27. exv1.setBackgroundResource(R.anim.explosion);
  28. exa1=(AnimationDrawable)exv1.getBackground();
  29. fl.addView(exv1);
  30. fl.setOnTouchListener(newLayoutListener());
  31. setContentView(fl);
  32. }
  33. classExplosionViewextendsImageView{
  34. publicExplosionView(Contextcontext){
  35. super(context);
  36. }
  37. //handlethelocationoftheexplosion
  38. publicvoidsetLocation(inttop,intleft){
  39. this.setFrame(left,top,left+40,top+40);
  40. }
  41. }
  42. classLayoutListenerimplementsOnTouchListener{
  43. publicbooleanonTouch(Viewv,MotionEventevent){
  44. //firstly,uhavetostoptheanimation,iftheanimation
  45. //isstarting,ucannotstartitagain!
  46. exv1.setVisibility(View.INVISIBLE);
  47. exa1.stop();
  48. floatx=event.getX();
  49. floaty=event.getY();
  50. exv1.setLocation((int)y-20,(int)x-20);
  51. exv1.setVisibility(View.VISIBLE);
  52. exa1.start();
  53. returnfalse;
  54. }
  55. }
  56. }
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 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) {
			//firstly, u have to stop the animation,if the animation
			//is starting ,u can not start it again!
			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;
		}
    	
    }
}

配合Android的SurfaceView,Animation可以实现很好的过渡效果,SurfaceView的用法很简单,可参考:

http://rayleung.javaeye.com/blog/420410

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值