簡單製作骰子動畫

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 21. 2014 10:47
buy individual viagra pills
footnote fromhome ktory debi falls tribes eminesciene <a href="r-forge.r-project.org/.../...ida.html">buy viagra finland</a> informa <a href="r-forge.r-project.org/.../...lia.html">buy cheap viagra australia</a> smartprofile <a href="r-forge.r-project.org/.../...ork.html">buy viagra in munich</a>.


April 21. 2014 10:49
 Buy Cialis
Assuming the blogger uses their home computer with a consistant IP address. How easy or difficult is it to find out their true identity?.


April 21. 2014 10:49
ambien
This video is ok? if you already know how to use templates.  It's a bit fast for the novice, I think.. . veejays-booksandmovies.blogspot   dot  com


April 21. 2014 10:55
cialis
Codeine Cups Remix -? Conflo


April 21. 2014 10:56
Click On this site
Hello! I could have sworn I've been to your blog before but after looking at a few of the posts I realized it's new to me. Nonetheless, I'm certainly pleased I stumbled upon it and I'll be bookmarking it and checking back often!


April 21. 2014 10:56
phentermine
You got? the sarcasm in my comment, right? If viagra works for women how come it's not approved for them? How does it work on them/. . Just curious.


April 21. 2014 10:56
VistaPrint Coupons
I think other web-site owners should take this web site as an model - very clean and wonderful style and design, in addition to the content. You are an expert in this topic!


April 21. 2014 10:56
Viagra
Edo pou? ftasame,min aporeis kai tin aitia an zitas na breis s auto to soma tis apantiseis tha breis...


April 21. 2014 10:57
beli sweater
Wow, what a video it is! In fact nice feature video, the lesson given in this video is genuinely informative.


April 21. 2014 10:59
hacker facebook
You completed certain good points there. I did a search on the matter and found a good number of persons will agree with your blog.


April 21. 2014 11:01
Suggested Webpage
Normally I don't read post on blogs, however I wish to say that this write-up very compelled me to check out and do so! Your writing style has been surprised me. Thank you, very great post.


April 21. 2014 11:01
siding Roofing
I think other website proprietors should take this web site as an model, very clean and great user genial style and design, let alone the content. You are an expert in this topic!


April 21. 2014 11:02
adipex
Any suggestions for building an eco-friendly house?


April 21. 2014 11:03
cialis
Hello. . If you want to add keyword on your blog which is developed on blogger just login your account and go in html there you can add your keyword meta tag.. . All the meta tag which you want you can save there. Other way is you can implement your own html too.


April 21. 2014 11:04
grosse-bite-gay
A lot of thanks for all of the work on this blog. My aunt loves making time for investigations and it's really simple to grasp why. We notice all about the lively means you make very important strategies by means of your web blog and in addition attract contribution from the others on this idea then our child has been understanding a whole lot. Take advantage of the remaining portion of the new year. You are carrying out a pretty cool job.


blues song new viagra commercial
tutoring overloading tapusin forefathers selesaifile refundable winname <a href="r-forge.r-project.org/.../...ne.html">best place to buy viagra in pattaya</a> declarez <a href="r-forge.r-project.org/.../...is.html">best real viagra prices</a> bujag <a href="r-forge.r-project.org/.../...uk.html">best viagra in india</a>.


April 21. 2014 11:16
additional reading
Hello, this weekend is pleasant for me, as this point in time i am reading this enormous educational article here at my house.


April 21. 2014 11:18
Read This
This article is pleasant and fruitful in support of all new PHP related web programmers; they have to study it and do the practice.


April 21. 2014 11:20
Meridia
My friends and I were arguing over how many tramadol it takes to kill a person and I would like some further opinions on this so we can solve this fight haha..


April 21. 2014 11:33
Viagra
When I made it out of the temple and got done talking to meridia I fell and took fall damage and died! Is this a glitch? If so how can I fix? it?


April 21. 2014 11:50
Viagra
What are some good slogans to use for my sports talk radio show?


April 21. 2014 11:57
viagra
movie at. iwantmovies.info


April 21. 2014 12:00
Suggested Resource site
This post concerning Search engine marketing is actually pleasant one, and the back links are really very useful to promote your web site, its also called SEO.


April 21. 2014 12:01
phentermine
Hi, so I have a blogger acount and a blog page set up, and I've heard that you can add music somehow? Can someone explain this-- something about pasting a code into something else?.


April 21. 2014 12:01
Viagra
I am looking both for blogs that give unbiased, balanced commentary on all issues or blogs that have a liberal or left-wing slant.  Thank you..


implant dentist Spokane Washington
It is truly a nice and useful piece of info. I’m glad that you just shared this useful info with us. Please stay us up to date like this. Thank you for sharing.


April 21. 2014 12:05
One Billion
I think this is among the most significant information for me.And i'm glad reading your article. But want to remark on few general things, The web site style is great, the articles is really nice : D. Good job, cheers


April 21. 2014 12:07
Viagra
Just want to share something. I need to submit my blogs to blog sites with so many people reading blogs. That's what I need most..


April 21. 2014 12:07
Rooms
Howdy! I simply want to give you a big thumbs up for the great info you have got here on this post. I’ll be coming back to your web site for more soon.


April 21. 2014 12:14
Viagra
Is it Ok to take soma and felxerill at the same time?


April 21. 2014 12:14
Viagra
what do I need to do to market my online business?


April 21. 2014 12:24
 viagra
What are some websites that let you make a website for free? Pickign a best answer?


April 21. 2014 12:27
Viagra
I think i might have done something wrong. After I did something to CSS my blogger template doesn't work. I cannot change my font, colors, background etc. I really need to, PLEASE PLEASE PLEASE someone help me Frown How can I do something to allow my blogger template designer work?.


April 21. 2014 12:32
Read the Full Posting
For most recent news you have to pay a quick visit the web and on internet I found this site as a finest web site for latest updates.


April 21. 2014 12:33
xbox live gratuit
I was wondering if you ever thought of changing the layout of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or 2 pictures. Maybe you could space it out better?


Search Engine Optimisation London
Howdy! This blog post couldn’t be written much better! Looking at this article reminds me of my previous roommate! He always kept preaching about this. I’ll send this information to him. Fairly certain he’s going to have a great read. Thanks for sharing!


April 21. 2014 12:33
Jasa SEO Budi Haryono
Nice post. I was checking constantly this weblog and I'm impressed! Very useful information specifically the final section Smile I handle such info much. I was looking for this particular information for a long time. Thank you and good luck.

My web blog ::  Jasa SEO Budi Haryono - http://www.budiharyono.name


April 21. 2014 12:35
xanax
Yes, however not everyone experiences the weight gain. I did, I gained two? stone while on Lexapro even though my eating or exercising habits didn't change. If you're taking or going to take antidepressants just keep an eye on your weight and take yourself off them if you do see an increase.


April 21. 2014 12:41
Xbox live gratuit
I’ve recently started a website, the information you provide on this site has helped me tremendously. Thanks  for all of your time & work.


April 21. 2014 12:41
viagra
LAWSUITS ARE STARTING AGAINST MERCK OVER PROPECIA !!!!. . The ASSHOLES at Merck knew that their drug (Propecia)? caused Impotence and? they still went? to market with their poison-pills !!!. . These pricks should face CRlMINAL-CHARGES? for their actions !!!!!


April 21. 2014 12:42
useful site
Wonderful goods from you, man. I have understand your stuff previous to and you're just too wonderful. I actually like what you've acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I cant wait to read much more from you. This is really a wonderful web site.


April 21. 2014 12:45
hqguestposting
Pretty! This was an incredibly wonderful post. Many thanks for providing this info.


April 21. 2014 12:49
Irwin Deuschel
clobetasol cream price <a href="http://is.gd/ofQzOK">clobetasol propionate skin discoloration</a> clobetasol propionate 0.05 cream price clobetasol price compare?


April 21. 2014 12:59
ambien
How can I prevent lorazepam/ativan withdrawal symptoms?


April 21. 2014 13:00
Leweb 2010
Quite wonderful posting. I became aware of your website and also desired to state that I’ve truly really liked searching your blog site posts. In any case I am subscribing to your own rss feed using this program . expecting you’re writing one more time shortly!


April 21. 2014 13:03
xanax
To place Orders for Safe acute painkiller pills for Back pain,Neck Pain etc. . Visit painkillerpills.webs.com. . Pills available,. Hydrocodone, Oxycodone,Oxycontin, Roxycodone, Percocet, Dilaudid,Diazepam. Xanax, Opana, and Actavis Promethezine cough syrup.. . Place your? orders and register to become a member.. Welcome


April 21. 2014 13:32
visit here
I was just seeking this info for a while. After 6 hours of continuous Googleing, at last I got it in your website. I wonder what's the Google's problem that does not rank this type of informative web sites closer to the top. Generally the top sites are full of garbage.


April 21. 2014 13:35
cialis
all pain medication (opiates)will do that.Although oxycodone didn't do it to me but darvacet? did!


April 21. 2014 13:35
One Billion
hi! , Everyone loves a person’s writing quite definitely! promote many of us keep up a correspondence more info on your own document for Yahoo? I need a pro for this dwelling to unravel this dilemma. Could be that is definitely a person! Looking forward to search an individual.


April 21. 2014 13:37
Janicki
I dugg some of you post as I thought  they were  very beneficial   very beneficial


April 21. 2014 13:51
for nybegynnere
As a blow owner I believe the subject matter here is reallymagnificent.  I thank you for your efforts. You should keep it up forever! Good Luck.


April 21. 2014 13:51
xanax
What are the effects of being on Ambien for a year?


April 21. 2014 13:52
One Billion
It’s the ideal time to produce a several blueprints for any long run this is time to feel special. We’ve read this offered in case I might I personally prefer to counsel you quite a few useful concerns and also assistance. You could possibly may generate subsequent content articles in regards to this document. I need to discover more troubles over it!


April 21. 2014 13:57
xanax
Does taking Adderall for a long period of time always cause acne?


April 21. 2014 13:57
plumbers leighton buzzard
Hello would you mind letting me know which hosting company you're utilizing? I've loaded your blog in 3 completely different browsers and I must say this blog loads a lot faster then most. Can you recommend a good web hosting provider at a honest price? Many thanks, I appreciate it!


April 21. 2014 13:58
viagra
Doctors are AWARE that PROPECIA causes PERMANENT IMPOTENCE ... BUT are continuing to PROMOTE the drug in order to CASH-IN on INCENTIVES from? MERCK !. . These doctors? are telling their FAMILIES? to? AVOID FINASTERIDE but NOT their PATIENTS !. . They are also REFUSING TO REPORT ADVERSE-REACTIONS to the FDA !!!. . This is very DISTURBlNG !!!. . l STRONGLY URGE the VICTIMS of PROPECIA to start PHYSICALLY CONFRONTlNG the doctors who prescribed that drug? !!!. . The? is NO OTHER CHOICE !!!


April 21. 2014 13:59
view website
Wow, that's what I was searching for, what a stuff! existing here at this blog, thanks admin of this web page.

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