--------------------------------------------------------前置工作----------------------------------------
先制定好一個 dice_anim1.xml 存放在 res/anim 裡面,並且將每張骰子的分解圖片放置在 res/drawable 裡
程式碼如下
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable = "@drawable/ndp" android:duration="100" />
<item android:drawable = "@drawable/ndp0" android:duration="100" />
<item android:drawable = "@drawable/ndp1" android:duration="100" />
<item android:drawable = "@drawable/ndp2" android:duration="100" />
<item android:drawable = "@drawable/ndp3" android:duration="100" />
<item android:drawable = "@drawable/ndp4" android:duration="100" />
<item android:drawable = "@drawable/ndp5" android:duration="100" />
<item android:drawable = "@drawable/ndp6" android:duration="100" />
<item android:drawable = "@drawable/ndp7" android:duration="100" />
<item android:drawable = "@drawable/ndp8" android:duration="100" />
<item android:drawable = "@drawable/ndp9" android:duration="100" />
<item android:drawable = "@drawable/nd1" android:duration="250" />
</animation-list>
每一個動畫的最後一張圖要隨著不同結果變更,所以要存成六個檔案
-----------------------------------------------主程式 NewGame.java-------------------------------------
在主程式裡加入。
changetodice = new Intent(); //加入新的意圖
changetodice.setClass(NewGame.this,throwdice.class); //等等受到震動時轉換
接下來加入sensor的功能,這部分可以參考一些書籍,在此就不詳述了,判定有擲動作裡放入以下程式碼
Random randnumber = new Random(); //隨機變數
int diceNumber =randnumber.nextInt(6)+1; //變成骰子的點數
bundle.putInt("DICE_RESULT", diceNumber); //將值讀入變數裡以供下個activity用
changetodice.putExtras(bundle); //將budle傳過去
startActivity(changetodice);
-----------------------------------------搖骰子程式 throwdice.java---------------------------------------
程式碼如下
public class throwdice extends Activity{
public AnimationDrawable diceani;
private int player;
@Override
public void onCreate(Bundle saveInstanceState){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(saveInstanceState);
setContentView(R.layout.throwdice);
Bundle bundle = this.getIntent().getExtras(); //get budle!
final int dice_number = bundle.getInt("DICE_RESULT");
ImageView dice = (ImageView)findViewById(R.id.Img2); // 這邊要先在 layout/throwdice.xml 加入一個 imageview
if (dice_number==1){
dice.setBackgroundResource(R.anim.dice_anim1); // 這邊的xml就要隨著不同的骰子結果變化
}
else if(dice_number==2){
dice.setBackgroundResource(R.anim.dice_anim2);
}
else if(dice_number==3){
dice.setBackgroundResource(R.anim.dice_anim3);
}
else if(dice_number==4){
dice.setBackgroundResource(R.anim.dice_anim4);
}
else if(dice_number==5){
dice.setBackgroundResource(R.anim.dice_anim5);
}
else if(dice_number==6){
dice.setBackgroundResource(R.anim.dice_anim6);
}
diceani = (AnimationDrawable)dice.getBackground();
Thread thread01 = new Thread(new Runnable() { //控制此activity的時間
@Override
public void run() {
try {
Thread.sleep(4000); //配合搖骰子的時間
finish(); //結束
} catch (Exception e) {
}
}
});
thread01.start();
//使用switch選擇點數並且對應到不同的執行工作
}
@Override
public void onWindowFocusChanged(boolean hasFocus){ //一開始要使用動畫必須加入這個函式
super.onWindowFocusChanged(hasFocus);
if(hasFocus==true){
diceani.start();
}
else{
diceani.stop();
}
}
}
這種方法只是最基本也是最笨的方法,如果使用OpenGL來實作骰子會漂亮更多,也會更多彈性。
附上骰子的分解圖
給大家參考一下~^^