Android遊戲開發:如何?爆炸效果

來源:互聯網
上載者:User

 

在做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>

接著是主程式碼: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;
}
}
}

配合Android的SurfaceView,Animation可以實現很好的過渡效果,SurfaceView的用法很簡單。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.