簡單製作骰子動畫

by Admin 22. July 2011 09:05

大家好,今天要來講在android中如何製作骰子動畫

--------------------------------------------------------前置工作----------------------------------------

先制定好一個 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來實作骰子會漂亮更多,也會更多彈性。

附上骰子的分解圖

給大家參考一下~^^

 

drawable.rar (77.06 kb)

Tags:

Comments


April 23. 2014 16:22
music theory lessons
Wow! This can be one particular of the most beneficial blogs We have ever arrive across on this subject. Basically Excellent. I am also a specialist in this topic therefore I can understand your effort.


April 23. 2014 16:48
Discover More
More Info Here


April 23. 2014 16:50
amateur
Do you have a spam issue on this site; I also am a blogger, and I was wondering your situation; many of us have created some nice methods and we are looking to swap techniques with others, be sure to shoot me an e-mail if interested.


April 23. 2014 16:50
go to my site
Thanks very nice blog!


April 23. 2014 16:58
cigars
Absolutely   pent   subject material , thanks  for information .


April 23. 2014 16:58
poker championship
Hey there would you mind letting me know which webhost you're working with? I've loaded your blog in 3 completely different web browsers and I must say this blog loads a lot faster then most. Can you recommend a good hosting provider at a honest price? Thanks, I appreciate it!


April 23. 2014 16:59
business profile search
Just wish to say your article is as amazing. The clearness to your publish is simply great and that i can think you are knowledgeable in this subject. Well along with your permission allow me to snatch your feed to keep up to date with drawing close post. Thank you a million and please continue the enjoyable work.


April 23. 2014 17:00
how to improve singing
Thanks for these tips. One thing I also believe is the fact that credit cards giving a 0% apr often entice consumers in with zero monthly interest, instant authorization and easy online balance transfers, nevertheless beware of the number one factor that will void your 0% easy streets annual percentage rate and also throw you out into the terrible house rapid.


April 23. 2014 17:04
resource for this article
visit this weblink


just click the up coming website
Additional Info


April 23. 2014 17:21
Furniture
Certainly I like your website, but you have to check the spelling on quite a few of your posts. A number of them are rife with spelling problems and I find it very bothersome to inform you. However I’ll definitely come again again!


April 23. 2014 17:21
Writers Centre Norwich
The next time I read a blog, I hope that it doesnt disappoint me as significantly as this one. I mean, I know it was my option to read, but I essentially thought youd have some thing intriguing to say. All I hear is often a bunch of whining about something that you could fix for those who werent too busy looking for attention.


April 23. 2014 17:22
Sectional
Thanx for a very informative site. Where else may I get that kind of info written in such a perfect way? I've a undertaking that I'm just now operating on, and I have been on the look out for such info.


April 23. 2014 17:24
Terri's Consign &amp; Design
I haven’t checked in here for some time since I thought it was getting boring, but the last few posts are really good quality so I guess I’ll add you back to my everyday bloglist. You deserve it my friend. Smile


April 23. 2014 17:29
Fairmont Designs
Simply want to say your article is as astounding. The clearness in your post is just excellent and i could assume you are an expert on this subject. Fine with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



TextBox

Tag cloud

    RecentPosts

    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2014 Program Paradise